Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use ${@+"$@"} in shell scripts?

Tags:

linux

posix

sh

An example:

foo() {
    for i in ${@+"$@"}; 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 "$@"; do? I'm sure there is some benefit, but can anyone make an example?

Thanks!

like image 552
Klaus Avatar asked Jul 05 '13 11:07

Klaus


1 Answers

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

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

like image 131
chepner Avatar answered Sep 22 '22 17:09

chepner