Accessing bash command line args $@ vs $*

匿名 (未验证) 提交于 2019-12-03 02:29:01

问题:

In many SO questions and bash tutorials I see that I can access command line args in bash scripts in two ways:

$ ~ >cat testargs.sh  #!/bin/bash  echo "you passed me" $* echo "you passed me" $@ 

Which results in:

$ ~> bash testargs.sh arg1 arg2 you passed me arg1 arg2 you passed me arg1 arg2 

What is the difference between $* and $@?
When should one use the former and when shall one use the latter?

回答1:

The difference appears when the special parameters are quoted. Let me illustrate the differences:

$ set -- "arg  1" "arg  2" "arg  3"  $ for word in $*; do echo "$word"; done arg 1 arg 2 arg 3  $ for word in $@; do echo "$word"; done arg 1 arg 2 arg 3  $ for word in "$*"; do echo "$word"; done arg  1 arg  2 arg  3  $ for word in "$@"; do echo "$word"; done arg  1 arg  2 arg  3 

one further example on the importance of quoting: note there are 2 spaces between "arg" and the number, but if I fail to quote $word:

$ for word in "$@"; do echo $word; done arg 1 arg 2 arg 3 

and in bash, "$@" is the "default" list to iterate over:

$ for word; do echo "$word"; done arg  1 arg  2 arg  3 


回答2:

A nice handy overview table from the Bash Hackers Wiki:

If the arguments are to be stored in a script variable and the arguments are expected to contain spaces, I wholeheartedly recommend employing a "$*" trick with the internal field separator $IFS set to tab.



回答3:

$*

Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special variable. That is, "$*" is equivalent to "$1c$2c...", where c is the first character of the value of the IFS variable. If IFS is unset, the parameters are separated by spaces. If IFS is null, the parameters are joined without intervening separators.

$@

Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word. That is, "$@" is equivalent to "$1" "$2" ... If the double-quoted expansion occurs within a word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the last parameter is joined with the last part of the original word. When there are no positional parameters, "$@" and $@ expand to nothing (i.e., they are removed).

Source: Bash man



回答4:

$@ is same as $*, but each parameter is a quoted string, that is, the parameters are passed on intact, without interpretation or expansion. This means, among other things, that each parameter in the argument list is seen as a separate word.

Of course, "$@" should be quoted.

http://tldp.org/LDP/abs/html/internalvariables.html#ARGLIST



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!