[Top][All Lists]

[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, 1 Feb 2005 20:54:14 +0000
User-agent: Mutt/1.5.6i

On Tue, Feb 01, 2005 at 02:20:10PM -0500, Chet Ramey wrote:
> 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.)

Ok, then. Thank you for the precisions.

There's still one obscur thing:

$ a() { echo $#; }
$ a=
$ a ${a+"$a"}
$ a ${a+ "$a" }

Sadly, that means that

IFS=" "; cmd ${opt+-x "$opt"}

is not reliable.

IFS=" "; cmd ${opt+-x "address@hidden"}
would be but is not POSIX.

set -- "$opt"
IFS=" "; cmd ${opt+-x "$@"}


eval cmd ${opt+'-x "$opt"'}

would be, but are a bit convoluted.

cmd ${opt+"-x"} ${opt+"$opt"}

seems to work, but from your explanation, I'm not too sure it should.

I must confess I can't see how having implemented it this way
can be useful to anything.

> > $ 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.

That was pdksh.

Note that's the same with dash (so probably also BSDs sh).

Another difference with pdksh:

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


reply via email to

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