bash question on nested braces
I was reading through the latest edition of Linux Programming Unleased. On page 717, the author gives an example:
Code:
$ echo c{ar,at,an}s That works on my Ubuntu system (bash 4.2.25(1)-release) exactly as the example states. However, the author then says that since braces can be nested, the command: Code:
$ echo c{a{r,t,n}}s c{ar}s c{at}s c{an}s Is this a bash bug? I get the expected output for the following command: Code:
echo c{a{r,t},an}s Code:
echo c{a{r,t,},a{b,n}} |
You probably have to wait for David.H to look at this, but I believe it is a subtle exception. The exception being that without a comma separated list the braces are interpreted literally.
I found this by doing the following: Code:
$ echo c{a{r,t,n},}s Code:
{ list; } |
There must be at least one comma or sequence expression.
Quote:
I added the bold highlighting. So you see that without a comma in your example, it is considered an "incorrectly formed brace expansion" and is "left unchanged." |
Perhaps the code above does work in some other shell like zsh?
But I think Beryllos has found the key to it in bash. A brace expansion can only be used inside another if it's completely contained inside a list sub-entry. It looks like your book is wrong. It appears that bash is reading it with the following three parts, with only the middle part being a valid brace expansion: Code:
c{a #beginning string Code:
echo c{a{r,t},an}s I've found the bash-hacker's page on it to be quite useful. http://wiki.bash-hackers.org/syntax/expansion/brace |
This is what i got. it wont work in bash shell...
Code:
$ echo $0 |
Brace expansion is only available in some advanced shells like bash, ksh, and probably zsh. It won't work in strictly posix-based shells like dash.
/bin/sh is your system's posix-supporting shell, which is usually a symlink to another interpreter like bash or dash. So it may work or it may not, depending on what that link is. And we've already explained the problems with the above line in bash (it does work, it just doesn't work as expected by the OP). But it is interesting to see that it apparently also works in c-shell environments, and apparently works as expected. |
The book is correct for old versions of bash.
Bash started to behave differently somewhere around the introduction of version 4. Code:
echo c{a{r,t,n}}s |
Even more interesting stuff to learn! I guess bash originally tried to emulate the csh style, but later went its own way for some reason.
Incidentally, I made an error in my last post. Posix does have the "{a,b,c}" list style of expansion, but it does not support the "{a..z}" range style. |
Works in bash 4.3.48
I revisited this today, and tried
Code:
echo c{a{r,t,n}}s Code:
cars cats cans |
Still the same for version 5.1.16
Code:
30-06-2022 17:56:07][~] |
Quote:
Code:
$ echo c{a{r,t,n}}s |
To reiterate what was stated in post #3 from June 2013 this behaviour is clearly described in the Brace Expansion documentation:
Quote:
If someone feels they have found a bug in a supported version of Bash, they should report it to GNU on the Bash bug mailing list, though I'm not sure 4.3 is supported. |
All times are GMT -5. The time now is 03:40 AM. |