what is difference between $@ and $* in UNIX Shell script
Answers were Sorted based on User's Feedback
Answer / yogesh malkoti
In a shell script :
$@: each quoated string treated as a separate argument
while gving at command line.
$*: stores the complete set of positional parameters as a
single string
Is This Answer Correct ? | 45 Yes | 3 No |
Answer / rajkapooor
$@ all quated string in command line treated as seprate
argument
$* stores all argument as a single argument
Is This Answer Correct ? | 12 Yes | 2 No |
Answer / 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 |
In which File is Static IP address given so that it becomes Permanent ?
what is work of dns
How do you run command every time a file is modified?
Explain command grouping in linux?
Explain about sh?
How do you create a blank file in linux?
How can we see the default gateway ip ini linux.
What is gnu in linux?
To move /home/ben/memos dir in /home/bob/memos, what is the result by mv /home/ben/memos /home/bob
How do I create a .txt file?
How much ram do I have linux?
Which command is used to find what is in your home directory?