当前位置: 动力学知识库 > 问答 > 编程问答 >

linux - Why use ${@+"[email protected]"} in shell scripts?

问题描述:

An example:

foo() {

for i in ${@+"[email protected]"}; do

echo $i

done

}

I understand that ${parameter+word} results in the expansion of word if parameter is set. But why not just use for i in "[email protected]"; do? I'm sure there is some benefit, but can anyone make an example?

Thanks!

网友答案:

According to the POSIX standard, "[email protected]" should expand to nothing if [email protected] is not set. However, the original Bourne shell expands this to a single empty string. ${@+"[email protected]"} will expand to nothing if [email protected] is not set in either shell.

Source: http://www.in-ulm.de/~mascheck/various/bourne_args/

网友答案:

when [email protected] is an empty string "", ${@+"[email protected]"} produce nothing, but "[email protected]" produce "", check it:

foo() {
    for i in ${@:+"[email protected]"}; do
        echo XXX: $i
    done
}

bar() {
    for i in "[email protected]"; do
        echo XXX: $i
    done
}

echo foo:
foo ""
echo bar:
bar ""

foo doesn't call echo, but bar calls it

分享给朋友:
您可能感兴趣的文章:
随机阅读: