[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: ${var:+foo "$empty"}
From: |
Stephane Chazelas |
Subject: |
Re: ${var:+foo "$empty"} |
Date: |
Tue, 8 Feb 2005 19:35:14 +0000 |
User-agent: |
Mutt/1.5.6i |
On Mon, Feb 07, 2005 at 05:21:44PM -0500, Chet Ramey wrote:
> > There's still one obscur thing:
> >
> > $ a() { echo $#; }
> > $ a=
> > $ a ${a+"$a"}
> > 1
> > $ a ${a+ "$a" }
> > 0
>
> That shouldn't be unexpected. First, keep in mind that the characters
> between the operator and the closing brace are, in effect, quoted, since
> they are processed as a single unit (but that `quoted' is the wrong term
> to use -- clear? ;-) ).
>
> Now, consider what happens to a double quoted variable expansion when
> it appears in the middle of a string: when expanded, if the expansion
> is empty, it simply disappears. For example, x"$a"y expands to `xy' when
> x is unset or null.
>
> When a variable expansion is wholly quoted, and the expansion is empty, the
> result is a null argument.
>
> So, the first example expands to "" (quoting is preserved). The second
> example expands to " " (unquoted), which is removed by word splitting.
[...]
Ok, I'll take your word that there exists a sensible reason,
then ;). Note that I get opposite results with ksh93:
$ a() { echo $#; }
$ a=
$ a ${a+ "$a" }
1
$ a ${a+"$a"}
0
I get 1 1 with pdksh and 0 0 with zsh in sh emulation (but then,
zsh has yet another /logic/ for it).
I guess I'll have to classify it for my own use as non-portable
reliably then.
--
Stéphane