1

Most makefile have this strange set of symbols '${1+"$@"}'. $@ is the makefile target filename. But why does, people try to use the complex symbol like '${1+"$@"}'?

2

1 Answer 1

7

The purpose of ${1+"$@"} for portability.

POSIX defined $@ would expand to nothing if there're no positional arguments. But original Bourne shell (/bin/sh in Solaris 10 and before) would expand it to empty string "". Using ${1+"$@"} is a work around for this, since when "$@" only expanded if $1 was set.

Unfortunately, this construct doesn't work in zsh 3.x and pre 4.3, because zsh performs word splitting on ${1+"$@"} in sh emulation mode (shwordsplit was set):

$ set -- '1 2' $ for i in ${1+"$@"}; do echo $i; done 1 2 

"$@" is handled properly:

$ set -- '1 2' $ for i in "$@"; do echo $i; done 1 2 

So alias -g '${1+"$@"}'='"$@"' makes sure word splitting never performed when using ${1+"$@"}. This allows the construct ${1+"$@"} to be used in zsh, as it will be replaced by "$@" before expansion (you must use exactly the character string ${1+"$@"}, with no leading or trailing word characters).


Another work around you can do, is completely ignore using "$@" if there're no positional arguments, by checking $#:

case $# in (0) func ;; (*) func "$@" ;; esac 

It took more works, but completely portable.

You must log in to answer this question.