UNIX 中的 $@ 和 $* 之间有什么区别? [英] What's the difference between $@ and $* in UNIX?
问题描述
$@
和 $*
在 UNIX 中有什么区别?当在脚本中回显时,它们似乎都产生相同的输出.
What's the difference between $@
and $*
in UNIX? When echoed in a script, they both seem to produce the same output.
推荐答案
一个区别在于它们如何处理输出中的 IFS 变量.
One difference is in how they handle the IFS variable on output.
#!/bin/sh
echo "unquoted asterisk " $*
echo "quoted asterisk $*"
echo "unquoted at " $@
echo "quoted at $@"
IFS="X"
echo "IFS is now $IFS"
echo "unquoted asterisk " $*
echo "quoted asterisk $*"
echo "unquoted at " $@
echo "quoted at $@"
如果你像这样运行:./demo abc def ghi
,你会得到这个输出:
If you run this like this: ./demo abc def ghi
, you get this output:
unquoted asterisk abc def ghi
quoted asterisk abc def ghi
unquoted at abc def ghi
quoted at abc def ghi
IFS is now X
unquoted asterisk abc def ghi
quoted asterisk abcXdefXghi
unquoted at abc def ghi
quoted at abc def ghi
请注意,在将 IFS
更改为X"后,(仅)带引号的星号"行在每个单词"之间显示一个 X.如果 IFS
的值包含多个字符,则仅使用第一个字符.
Notice that (only) the "quoted asterisk" line shows an X between each "word" after IFS
is changed to "X". If the value of IFS
contains multiple characters, only the first character is used for this purpose.
此功能也可用于其他数组:
This feature can also be used for other arrays:
$ array=(123 456 789)
$ saveIFS=$IFS; IFS="|"
$ echo "${array[*]}"
123|456|789
$ IFS=$saveIFS
这篇关于UNIX 中的 $@ 和 $* 之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!