APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Bash 3.00 brace expansion

Ah, finally: no more of


for i in 1 2 3 4 5 6 7 8 9 0
do
.. whatever
done
 

With Bash 3.0, we now have brace expansion for lists:

for i in {1..10}  
do 
 echo $i 
done
 

And, happily:

for i in {10..1}  
do 
 echo $i 
done
 

Prior to this, we sometimes used "seq", which could result in such awful things as:

for i in `seq 65 69`; do echo -e `printf '\\%03o' $i`; done
 

Ugly. The Bash 3.0 way is so much cleaner:

for i in {A..E}; do echo $i;done
 

This also works for more traditional brace expansion use:

$  echo foo{1..4}bar
foo1bar foo2bar foo3bar foo4bar
# used to be done as
$ echo foo{1,2,3,4}bar
foo1bar foo2bar foo3bar foo4bar
 

Nesting of course still works:

echo foo{bar{1..3},-is-done}
foobar1 foobar2 foobar3 foo-is-done
 

and you can use it for such tasks as:

mkdir {A..D}{1..3}{0..9} 
 

which would have required nested loops before Bash 3.0.



Got something to add? Send me email.





(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> Bash 3.00 brace expansion

7 comments



Increase ad revenue 50-250% with Ezoic


More Articles by

Find me on Google+

© Tony Lawrence







Mon Oct 3 07:31:46 2005: 1149   drag


Oh.. that's nice. (I am sure somebody will come along and point out that ksh or other 'alternative' shell had it first, but still this is cool) :P







Mon Oct 3 13:09:30 2005: 1151   infinity


never knew that this kind of facility was available.
thanks!



Mon Oct 3 13:20:49 2005: 1152   TonyLawrence

gravatar
Well, nested brace expansion has worked before now, but the list expansion in braces is new. Combining both is pretty darn powerful as you see above..



Wed Apr 15 14:24:41 2009: 6177   anonymous

gravatar
Hi,

Is there anyway to specify an increment in the brace expansion?



Wed Apr 15 15:51:46 2009: 6178   TonyLawrence

gravatar
Yes, you can use a numeric increment; see (link)



Tue Nov 2 08:10:52 2010: 9087   Allan

gravatar


I am guessing you meant

mkdir {A..D}{1..3}{0..9}

instead of

mkdir {A..D}{1..3}{0-9}

for the last example?



Tue Nov 2 10:15:22 2010: 9089   TonyLawrence

gravatar


Yes, thank you.

------------------------
Kerio Samepage


Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us





An adversary capable of implanting the right virus or accessing the right terminal can cause massive damage. (George Tenet, director of the U.S. Central Intelligence Agency)





This post tagged: