Issue
Who can simply explain
- what is the difference between $* and $@?
- Why there are two variables for same content as above?
Solution
Aside from the difference as described in the technical documents, it is best shown using some examples:
Lets assume we have four shell scripts, test1.sh
:
#!/bin/bash
rm $*
test2.sh
:
#!/bin/bash
rm "$*"
test3.sh
:
#!/bin/bash
rm $@
test4.sh
:
#!/bin/bash
rm "$@"
(I am using rm
here instead of echo
, because with echo
, one can not see the difference)
We call all of them with the following commandline, in a directory which is otherwise empty:
./testX.sh "Hello World" Foo Bar
For test1.sh
and test3.sh
, we receive the following output:
rm: cannot remove ‘Hello’: No such file or directory
rm: cannot remove ‘World’: No such file or directory
rm: cannot remove ‘Foo’: No such file or directory
rm: cannot remove ‘Bar’: No such file or directory
This means, the arguments are taken as a whole string, joined with spaces, and then reparsed as arguments and passed to the command. This is generally not helpful when forwarding arguments to another command.
With test2.sh
, we get:
rm: cannot remove ‘Hello World Foo Bar’: No such file or directory
So we have the same as for test{1,3}.sh
, but this time, the result is passed as one argument.
test4.sh
has something new:
rm: cannot remove ‘Hello World’: No such file or directory
rm: cannot remove ‘Foo’: No such file or directory
rm: cannot remove ‘Bar’: No such file or directory
This implies that the arguments are passed in a manner equivalent to how they were passed to the the script. This is helpful when passing arguments to other commands.
The difference is subtle, but will bite you when passing arguments to commands which expect information at certain points in the command line and when spaces take part in the game. This is in fact a good example of one of the many pitfalls of most shells.
Answered By - Jonas Schäfer Answer Checked By - Mary Flores (WPSolving Volunteer)