[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] bash wrapper: using $@ instead of ${1+"$@"}

From: Bob Proulx
Subject: Re: [Help-bash] bash wrapper: using $@ instead of ${1+"$@"}
Date: Fri, 11 Dec 2015 23:23:53 -0700
User-agent: Mutt/1.5.24 (2015-08-30)

Patrick Schleizer wrote:
> A question regarding:
> It still recommends:
> ${1+"$@"}

I think that is still a good idea for maximum portability.  But if you
are not ever interacting with an older Bourne shell then not required
in that complete long form.

> Assumed, that only recent versions of bash are in use, then one can
> safely use the simpler notation of:
> $@
> i.e. simply
> /path/to/wrapper $@
> Should work fine in all cases?

No.  It must use quotes around the address@hidden  You always need at least "$@"
with the quotes.  The bash manual says:

  Special Parameters
   @ Expands to the positional parameters, starting from  one.   When
     the  expansion  occurs  within  double  quotes,  each  parameter
     expands to a separate word.  That is, "$@" is equivalent to "$1"
     "$2"  ...
     When  there  are no positional parameters, "$@" and $@ expand to
     nothing (i.e., they are removed).

See that part "When the expansion occurs within double quotes"?  That
means that if the expansion is NOT quoted then it does not have this
magic property.  Therefore you must quote it.

The question about the ${1+ part has to do with portability.  The
classic Bourne shell still present on many legacy Unix systems for
/bin/sh requires that ${1+ part to work correctly for #!/bin/sh
scripts.  Otherwise without any arguments "$@" becomes "" and creates
an empty argument when one should not be there.

The POSIX standard requires this same behavior and therefore the POSIX
shell /bin/sh also expands this way and "$@" is okay to use.  But if
you care about portability to older systems with a Bourne shell
/bin/sh then ${1+"$@"} is required for correct operation.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]