是什么&QUOT之间的区别; $ @"和" $ * QUOT;在Bash的?
在我看来,它们都存储所有的命令行参数。
It seems to me that they both store all the command-line arguments.
那么,有没有两者之间的区别吗?
So is there a difference between the two?
的区别很微妙; $ *
创建了一个由 $ IFS
变量分隔的一个参数,而$ @
将业务拓展到不同的参数。作为一个例子,考虑:
The difference is subtle; "$*"
creates one argument separated by the $IFS
variable, while "$@"
will expand into separate arguments. As an example, consider:
for i in "$@"; do echo "@ '$i'"; done
for i in "$*"; do echo "* '$i'"; done
当有多个参数运行:
./testvar foo bar baz 'long arg'
@ 'foo'
@ 'bar'
@ 'baz'
@ 'long arg'
* 'foo bar baz long arg'
有关详细信息:
http://www.gnu.org/software/bash/manual/bashref.html#Special-Parameters
$ *
扩展为位置参数,从1开始。当扩展双引号内发生,它扩展到由IFS特殊变量的第一个字符分隔每个参数的值一个字。也就是说,$ *等同于$ 1C $ 2C ...,其中c是IFS变量的值的第一个字符。如果IFS没有设置,参数用空格分隔。如果IFS为空,参数结合而不分隔器。
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.
$ @
扩展为位置参数,从1开始。当双引号里展开时,每个参数扩展到一个单独的词。也就是说,$ @等同于$ 1$ 2....如果一个字内发生双引号扩展,第一个参数的膨胀接合与原始字的开始部分,并且最后一个参数的扩张接合与原始字的最后一部分。当有没有位置参数,$ @和$ @扩大到什么(即,它们被删除)。
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).