0

What is the difference between $* and $@ in bash?

From the Bash doc,

enter image description here

The above description makes my brain hurt.
So I tried to figure out the difference with the following script ("myscript"):

echo $*
echo $@

If I run "myscript -x ARG1", the output is

-x ARG1
-x ARG1

That is, both produce the same output.

What am I missing here?

DontPanic
  • 2,164
  • 5
  • 29
  • 56
  • Almost always, the correct answer is: `"$@"` (don't forget the double-quotes). All other versions, with `$*` or without the double-quotes, lead to weird parsing of one sort or another. *Understanding* the weird parsing is not necessary, just remember: `"$@"`. Also, don't try to assign a list of arguments to a plain variable, like `args="$@"`, because that also causes confusion. – Gordon Davisson Feb 23 '22 at 21:01
  • Both lines in `myscript` are bad. Use [Shellcheck](https://www.shellcheck.net/) to find out what is wrong and how to fix it. Very many shell code problems can be found that way. – pjh Feb 23 '22 at 22:36

0 Answers0