On 2001.09.16, in <[EMAIL PROTECTED]>,
"David T-G" <[EMAIL PROTECTED]> wrote:
>
> % Quoting FreeBSD's "man sh" [from the listing of special parameters]:
> % | @ Expands to the positional parameters, starting from one.
> % | When the expansion occurs within double-quotes, each
>
> Aha! I don't use BSD (yet), so I don't have access to that, or at least
> hadn't thought to search the web for a copy of the freebsd sh man page.
> My linux system doesn't even have a man page for sh, preferring info or
> whatever it is, and Solaris doesn't mention it at all.
Solaris does, just differently. The section on ${parameter} says first
that if the parameter is "*" or "@", all positional parameters are
substituted. Later, in the section on quoting, it says that if "$@"
is encased in double-quotes, then the pos'nal params are quoted, and
separated by unquoted spaces. Searching the manual for "@" shows this.
> % (Prominent example: just now while writing this mail, i saw for the
> % first time how ${1+"$@"} works. Ingenious approach, even if a bit
>
> I see that "$@" protects argument expansion; what does the 1 mean? And I
> don't have a Solaris box handy to look in a man page to reference what +
> means; I only use :- on a regular basis.
>From Solaris's sh(1):
${parameter:+word}
If parameter is set and is non-null, substitute word;
otherwise substitute nothing.
...
If the colon (:) is omitted from the above expressions, the
shell only checks whether parameter is set or not.
":+" is the flipside of ":-" -- ":-" substitutes if parameter doesn't
have a non-null value, but ":+" substitutes if it does.
So ${1+"$@"} means to substitute all args, quoted, if there is a first
argument (null or not). It's equivalent to "$@" alone on most systems
I've seen. ${1:+"$@"} is not equivalent.
> % redundant. Most probably in the context of the tip you found it in it
> % was a workaround for a shell that didn't handle a plain "$@" correctly?)
It's possible that some shells might issue an error evaluating "$@" if
no arguments are present. csh(1) does this by design when evaluating
any variable. This is the kind of behavior I wouldn't be surprised to
see in older versions of AIX, for example; it's sometimes been fairly
strict in its response to undefined conditions. Most systems substitute
nothing if there are no args. UNIX98 requires this, but I can't say with
certainty that prior versions of this standards family require anything
particular, so ${1+"$@"} might well be needed for backward compatibility
to POSIX or XPG or something. I don't know where to find these standards
online; the Open Group no longer offer previous editions to SUS.
--
-D. [EMAIL PROTECTED] NSIT University of Chicago