[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] What does this idiom mean
From: |
Eric Blake |
Subject: |
Re: [Help-bash] What does this idiom mean |
Date: |
Wed, 08 Feb 2012 12:52:21 -0700 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:10.0) Gecko/20120131 Thunderbird/10.0 |
On 02/08/2012 12:24 PM, Bill Gradwohl wrote:
> Somewhere, I believe, I saw the following:
>
> address@hidden@}
That says, if the variable on the left of + is set, then replace the
${...} with the expansion to the right of the +; if the variable on the
left of + is not set, then expand to nothing.
But this is redundant as written - either $@ is set, so you expand to $@
in the first place; or $@ is not set, and you expand to nothing; and
that can just as easily be written as:
$@
However, it is more likely that you've seen the idiom ${1+"$@"}.
Although the same arguments apply about redundancy (if $@ is set, then
$1 must be set), there is a _real_ reason for doing this in portable
code, which is that some older shells that don't quite comply with POSIX
misinterpret
"$@"
and generate an empty string if $@ is unset, rather than the POSIX
behavior of eliding the entire argument ("$@" is the only special
variable like that). By separating the "$@" to only be expanded if
there are arguments, you can omit the misinterpreted empty argument on
those older shells.
> I understand what address@hidden is but everything from the + onward is a
> mystery.
See the bash manual, under "Parameter Expansion", for several useful
conditional expansion forms:
${var:+value}
${var+value}
${var:-value}
${var-value}
${var:?value}
${var?value}
${var:=value}
${var=value}
--
Eric Blake address@hidden +1-919-301-3266
Libvirt virtualization library http://libvirt.org
signature.asc
Description: OpenPGP digital signature