Tuesday, April 8, 2008

Bash Brace Expansion

I have to give a little shout out to Bash brace expansion. This is one of the neat little toys that I rarely see mentioned. The really quick summary reads like this:
$ echo test_{foo,bar,baz}
test_foo test_bar test_baz
which is useful enough, but where it really shines is in sequence expansion. The idiomatic Bash For loop I've often seen is something like:
$ for i in `seq 1 10`; do echo $i; done
which is just nasty. Compare to:
$ for i in {1..10}; do echo $i; done
which is nicer, but even more nice is:
$ echo {1..10} | tr ' ' '\n'

And of course, something like this this comes in really handy:
$ wget www.somewhere.com/{a,b}{1..9}.jpg
Not sure what I would use that for... ;)

No comments: