bug-bash
[Top][All Lists]
Advanced

[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: Mon, 24 Jan 2005 10:15:12 +0000
User-agent: Mutt/1.5.6i

On Sun, Jan 23, 2005 at 01:16:32PM -0500, Chet Ramey wrote:
> Stephane Chazelas wrote:
> >Hi,
> >
> >$ bash -c 'printf "<%s>\n" ${$:+a "$a"}'
> ><a>
> >$ ksh -c 'printf "<%s>\n" ${$:+a "$a"}'
> ><a>
> ><>
> >$ dash -c 'printf "<%s>\n" ${$:+a "$a"}'
> ><a>
> ><>
> 
> 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've seen:

cmd ${var+-o "$var"}

used in many scripts, but I hadn't realized it was word splitting
that was involved until recently (I thought it was token parsed
a second time because of the quote handling).

The above command line works with every shell except with bash
and ksh93 when $var is empty (as long as neither "-" nor "o" are
in IFS and as long as " " is in IFS).

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

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

?

-- 
Stéphane





reply via email to

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