bug-bash
[Top][All Lists]
Advanced

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

Re: Brace expansion bug


From: Dan Douglas
Subject: Re: Brace expansion bug
Date: Mon, 26 Mar 2012 08:56:02 -0500
User-agent: KMail/4.7.4 (Linux/3.1.6-pf; KDE/4.7.4; x86_64; ; )

On Monday, March 26, 2012 01:44:58 PM you wrote:
> Dan Douglas <ormaaj@gmail.com> writes:
> > Hi, hopefully a self-explanatory one today:
> >     ~ $ ( set -x -- {a..c}; echo "${*-"{1..3}"}" )
> >     + echo 'a b c' 'a b c' 'a b c'
> >     a b c a b c a b c
> >     
> >     ~ $ ( set -x -- {a..c}; echo "${*/"{1..3}"/$*}" )
> >     + echo 'a b c' 'a b c' 'a b c'
> >     a b c a b c a b c
> 
> *Note (bash) Brace Expansion::
> 
>    Brace expansion is performed before any other expansions, and any
> characters special to other expansions are preserved in the result.  It
> is strictly textual.  Bash does not apply any syntactic interpretation
> to the context of the expansion or the text between the braces.  To
> avoid conflicts with parameter expansion, the string `${' is not
> considered eligible for brace expansion.
> 
> Andreas.

No other shell I have to test with that has brace expansion behaves this way. 
Is this intentional? There's a trivial workaround by escaping the first brace,  
but expansion order shouldn't be relevant here, this is about the parsing and 
token recognition steps. In order for brace expansion to occur to begin with 
it would have to first be recognized as such.

Ordinarily the inner and outer quotes of the alternate argument expansions are 
processed separately so that in the case of an unquoted expansion you can have 
situations like ${x+"$y" "$z"} which potentially expands to zero or two words 
depending on the circumstance. If there's a brace expansion detected, the 
function of the quotes is modified and the brace expansion becomes stronger 
from a parsing standpoint. This seems counter-intuitive.

Don't know how much I'm allowed to quote here, but a quick read of the POSIX 
parsing rules and parameter expansion sections suggest to me that  the start 
of the parameter expansion should be the most important factor, and that 
nested quotes and braces are counted as part of the parameter expansion and 
escape at least any closing braces (doesn't mention opening brace). They do 
seem to be emphasizing the point that there's nesting going on and that the 
shell should try to consider the parameter expansion as a whole first.

This is hard to interpret, it was obviously not written to take brace 
expansion into account. The Bash manpage does specifically omit brace expansion 
from evaluation of "word" which I suppose should be a clue that it's 
intentional.

-- 
Dan Douglas

Attachment: signature.asc
Description: This is a digitally signed message part.


reply via email to

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