Bash 中的 `$*` 和 `$@` 有什么区别

What is difference between `$*` an `$@` in 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.

这里有历史的发展。发现$*不够用,于是引入$@来代替。仍然有 $* 有用的情况;但除非你特别想分解引用的标记,否则你应该避免它。