Bash Brace Expansion by Anthony Lawrence
2008/04/20The new tricks Bash has picked up in 3.0 are exciting and useful, but simple brace expansion has been available for some time now, and yet we seldom see it used. I suppose that's because the need doesn't come up too often. The classic example from the Bash man page is:
mkdir /usr/local/src/bash/{old,new,dist,bugs}which expands to
mkdir /usr/local/src/bash/old /usr/local/src/bash/new /usr/local/src/bash/dist /usr/local/src/bash/bugsThe man page also gives a more complex example showing nested braces:
chown root /usr/{ucb/{ex,edit},lib/{ex?.?*,how_ex}}That's a little harder to follow, but if you replace "chown root" with "echo" you can see it:
$ echo /usr/{ucb/{ex,edit},lib/{ex?.?*,how_ex}} /usr/ucb/ex /usr/ucb/edit /usr/lib/ex?.?* /usr/lib/how_exThere are other possibities beyond nesting:
$ echo foo{1,2}{a,b} foo1a foo1b foo2a foo2bAnd did you know that you can leave out the arguments? As long as you have the comma, this works:
$ echo foo{,}{,} foo foo foo fooAnd so does this:
$ echo foo{,,,} foo foo foo fooBraces can get pretty complex:
$ echo foo{1,2,3,4}bar{7,8{foo,bar}} foo1bar7 foo1bar8foo foo1bar8bar foo2bar7 foo2bar8foo foo2bar8bar foo3bar7 foo3bar8foo foo3bar8bar foo4bar7 foo4bar8foo foo4bar8bar # with 3.0 bash the "foo{1,2,3.4}" can be "foo{1..4}"But when would you ever need anything like that?