bash脚本中$ @和$ *之间的区别 [英] difference between $@ and $* in bash script
本文介绍了bash脚本中$ @和$ *之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面有4个bash片段.我用./script.sh a b c
There are 4 bash snippets below. I call them with ./script.sh a b c
for arg in $@; do
echo "$arg"
done ## output "a\nb\nc"
for arg in "$@"; do
echo "$arg"
done ## output "a\nb\nc" -- I don't know why
for arg in $*; do
echo "$arg"
done ## output "a\nb\nc"
for arg in "$*"; do
echo "$arg"
done ## output "abc"
我不知道$@
和$*
之间的确切区别是什么,
而且我认为"$@"
和"$*"
应该相同,但事实并非如此.为什么?
I don't know what is the exact difference between $@
and $*
,
and I think "$@"
and "$*"
should be the same, but they are not. Why?
推荐答案
如果您有脚本foo.sh
:
asterisk "$*"
at-sign "$@"
并调用:
./foo.sh "a a" "b b" "c c"
等效于:
asterisk "a a b b c c"
at-sign "a a" "b b" "c c"
没有引号,它们是相同的:
Without the quotes, they're the same:
asterisk $*
at-sign $@
将等同于:
asterisk "a" "a" "b" "b" "c" "c"
at-sign "a" "a" "b" "b" "c" "c"
这篇关于bash脚本中$ @和$ *之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文