what is difference between $@ and $* in UNIX Shell script

Answer Posted / alf55

There is a difference between using $@ and using "$@". The
first is the same as using $*, while the latter is what was
being described ad $@. It only handles the arguments
correctly when used as "$@". However, you will not see where
the arguments are changing in its simple usage in a print.

echo "arguments are:"; for arg in "$@"; do echo "
${arg}"; done

Will show each argument on a new line indented by four spaces.

Here is an example:
[code]
bash$ function show_simple_args
> {
> echo "There are $# arguments passed, can you find
them correctly?"
> echo "using \$*:"
> echo $*
> echo "using \$@:"
> echo $@
> echo "using \"\$@\":"
> echo "$@"
> echo "using for loop with \$*:"
> echo "arguments are:"; for arg in $*; do echo "
${arg}"; done
> echo "using for loop with \$@:"
> echo "arguments are:"; for arg in $@; do echo "
${arg}"; done
> echo "using for loop with \"\$@\":"
> echo "arguments are:"; for arg in "$@"; do echo "
${arg}"; done
> }
bash$
bash$ show_simple_args "arg 1" "arg 2" "arg 3" "arg 4"
There are 4 arguments passed, can you find them correctly?
using $*:
arg 1 arg 2 arg 3 arg 4
using $@:
arg 1 arg 2 arg 3 arg 4
using "$@":
arg 1 arg 2 arg 3 arg 4
using for loop with $*:
arguments are:
arg
1
arg
2
arg
3
arg
4
using for loop with $@:
arguments are:
arg
1
arg
2
arg
3
arg
4
using for loop with "$@":
arguments are:
arg 1
arg 2
arg 3
arg 4
bash$
[/code]

Is This Answer Correct ?    9 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is history command in linux?

556


What is difference between comm and CMP command?

527


Is llvm a compiler?

547


What does sh do in linux?

509


What is devops model?

524






How do I remove a soft link in linux?

517


Information technology is widely used in

1495


Which of the Commands delete the files from the /tmp directory, issued by non-root user?

653


How will you suspend a running process and put it in the background?

1025


How much ram do I have linux?

540


How to know linux os is 32 bit or 64 bit?

535


You suspect that you have two commands with the same name as the command is not producing the expected results. What command can you use to determine thelocation of the command being run?

509


How does grep work in linux?

523


How do you save a file in linux?

570


In order to improve your system’s security you decide to implement shadow passwords. What command should you use?

537