参见英文答案 >
What’s the difference between $@ and $* in UNIX?4个
当我想要bash函数的所有参数时,我总是使用$@但最近我发现$*也以相同的方式工作,它也可以用作数组索引.
当我想要bash函数的所有参数时,我总是使用$@但最近我发现$*也以相同的方式工作,它也可以用作数组索引.
我的问题是Bash中$*和$@之间的区别是什么?我应该选择哪一个?
Bash manual在这个主题上非常明确:
$*
All of the positional parameters,seen as a single word.
Note:
$*
must be quoted.
$@
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.Note: Of course,
$@
should be quoted.