问题描述:

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

相关阅读:
Top