What is the difference between `$ *` `` $@ `in Bash

I always use $@ when I need all the arguments to the bash function, but recently I just discovered that $* also works in the same way and can also be used as an array index.

My question is: what is the difference between $* a $@ in Bash? and which one should I prefer?

+5
source share
2 answers

The bash guide is pretty clear on this topic:

  • $*

    All positional parameters considered as one word.

    Note: $* must be specified.

  • $@

    Same as $* , but each parameter is a string with quotes, that is, the parameters are passed unchanged, without interpretation or extension. This means, among other things, that each parameter in the argument list is treated as a separate word.

    Note. Of course, $@ should be indicated.

+7
source

There is a historical development. $* turned out to be insufficient, and therefore $@ was introduced to replace it. There are still situations where $* is useful; but if you specifically do not want to break the quoted tokens, you should avoid this.

+2
source

All Articles