【发布时间】:2016-03-07 13:02:15
【问题描述】:
在将 $@ 和 $* 传递给函数时,我很难理解它们之间的区别。
示例如下:
function a {
echo "-$1-" "-$2-" "-$3-";
}
function b {
a "$@"
}
function c {
a "$*"
}
如果调用:
$ b "hello world" "bye world" "xxx"
打印出来:
-hello world- -bye world- -xxx-
如果调用:
$ c "hello world" "bye world" "xxx"
打印出来:
$ c "hello world" "bye world" "xxx"
-hello world bye world xxx- -- --
发生了什么?我无法理解差异和出了什么问题。
【问题讨论】:
-
"$@" 字符串的数量与参数的数量一样多; "$*" 是一个字符串。有很多问题涵盖了这一点——我很快就会找到一个。
-
感谢@JonathanLeffler,这是一本很好的阅读材料。
标签: bash shell command-line arguments parameter-passing