$@
почти совпадает с $*
, оба означают «все аргументы командной строки».Они часто используются для простой передачи всех аргументов другой программе (таким образом, формируя оболочку вокруг этой другой программы).
Разница между двумя синтаксисами проявляется, когда у вас есть аргумент с пробелами в нем (например) ипоместите $@
в двойные кавычки:
wrappedProgram "$@"
# ^^^ this is correct and will hand over all arguments in the way
# we received them, i. e. as several arguments, each of them
# containing all the spaces and other uglinesses they have.
wrappedProgram "$*"
# ^^^ this will hand over exactly one argument, containing all
# original arguments, separated by single spaces.
wrappedProgram $*
# ^^^ this will join all arguments by single spaces as well and
# will then split the string as the shell does on the command
# line, thus it will split an argument containing spaces into
# several arguments.
Пример: вызов
wrapper "one two three" four five "six seven"
приведет к:
"$@": wrappedProgram "one two three" four five "six seven"
"$*": wrappedProgram "one two three four five six seven"
^^^^ These spaces are part of the first
argument and are not changed.
$*: wrappedProgram one two three four five six seven