bug-bash
[Top][All Lists]
Advanced

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

Re: ${var:+foo "$empty"}


From: Chet Ramey
Subject: Re: ${var:+foo "$empty"}
Date: Tue, 1 Feb 2005 14:20:10 -0500

> On Sun, Jan 23, 2005 at 01:16:32PM -0500, Chet Ramey wrote:

> > I believe that bash is right.  Posix says explicitly that the word
> > on the rhs of the `+' does not undergo word splitting when it is
> > used.  I do not believe that Posix intended word splitting to be
> > performed on the result.
> [...]
> 
> Well, then bash fails as well as any other shell (ksh, dash,
> ksh93 at least).
> 
> $ bash -c 'IFS=,; printf "<%s>\n" ${$:+a,b}'
> <a>
> <b>

I think you misunderstood what I meant by `result'.  Posix says that the result
of the expansions performed on the rhs of such an expression do not undergo
word splitting.

If the entire parameter expansion is not surrounded by double quotes, then
word splitting is performed.  Quoting inside and outside ${...} is essentially
independent (and, frankly, something that different shells do differently --
beware).

The word on the rhs of an operator like `:-' undergoes tilde expansion,
parameter expansion, command substitution, and arithmetic expansion.  It does
not undergo word splitting.  Non-empty expansions within double quotes have
quoting preserved internally for eventual handling based on the entire ${...}
expansion. (The non-empty part is for backwards compatibility with the Bourne
shell, which did things slightly differently.  ksh88 and ksh93 do the same
thing as bash.)

In the above example, the rhs expands to <a,b>, which, since the ${...}
expansion is not double-quoted, undergoes word splitting and becomes
<a> <b>.

> $ bash -c 'IFS=" "; a=; printf "<%s>\n" ${a+-o "$a"}' - a b ''
> <-o>

The result is "a ", which, when split, turns into "a".  Empty quoted
expansions disappear, as if the quoting were not present.

> ~$ bash -c 'IFS=" "; a="foo bar"; printf "<%s>\n" ${a+-o "$a"}' - a b ''
> <-o>
> <foo bar>

Non-empty quoted strings have quoting preserved.

> ~$ bash -c 'IFS=" "; a="foo bar"; printf "<%s>\n" ${a+-o $a}' - a b ''
> <-o>
> <foo>
> <bar>

Unquoted expansions are split.

> $ ksh -c 'IFS=" "; a=; printf "<%s>\n" ${a+-o "$a"}' - a b ''
> <-o>
> <>

I don't know what `ksh' is on your machine, but bash and ksh93 return
<-o>, as in your first example.

> Do you think, that POSIX would require that the results would
> be:
> 1:<-o >
> 2:<-o foo bar>
> 3:<-o foo>
>   <bar>

A case could be made for that; that's almost what the BSD sh does.  I
don't happen to agree. 

If you want to take this farther, you could request an interpretation
from the austin group.

Chet

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
( ``Discere est Dolere'' -- chet )
                                                Live...Laugh...Love
Chet Ramey, ITS, CWRU    address@hidden    http://tiswww.tis.cwru.edu/~chet/




reply via email to

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