Bash Brace Expansion
May 30th, 2008 by Mitch Frazier in
Bash brace expansion is used to generate stings at the command line or in a shell script. The syntax for brace expansion consists of either a sequence specification or a comma separated list of items inside curly braces "{}". A sequence consists of a starting and ending item separated by two periods "..".
Some examples and what they expand to:
{aa,bb,cc,dd} => aa bb cc dd
{0..12} => 0 1 2 3 4 5 6 7 8 9 10 11 12
{3..-2} => 3 2 1 0 -1 -2
{a..g} => a b c d e f g
{g..a} => g f e d c b a
If the brace expansion has a prefix or suffix string then
those strings are included in the expansion:
a{0..3}b => a0b a1b a2b a3b
Brace expansions can be nested:
{a,b{1..3},c} => a b1 b2 b3 c
Counted loops in bash can be implemented a number of ways without brace expansion:
# Three expression for loop:
for (( i = 0; i < 20; i++ ))
do
echo $i
done
# While loop:
i=0
while [[ $i -lt 20 ]]
do
echo $i
let i++
done
# For loop using seq:
for i in $(seq 0 19)
do
echo $i
done
for i in {0..19}
do
echo $i
done
for i in {a..z}
do
echo $i
done
Brace expansion can also be useful when passing multiple long pathnames to a command. Instead of typing:
# rm /a/long/path/foo /a/long/path/barYou can simply type:
# rm /a/long/path/{foo,bar}
Brace expansion is enabled via the "set -B" command and the "-B" command line option to the shell and disabled via "set +B" and "+B" on the command line.
__________________________Mitch Frazier is an Associate Editor for Linux Journal and the Web Editor for linuxjournal.com.
Special Magazine Offer -- Free Gift with Subscription
Receive a free digital copy of Linux Journal's System Administration Special Edition as well as instant online access to current and past issues. CLICK HERE for offer
Linux Journal: delivering readers the advice and inspiration they need to get the most out of their Linux systems since 1994.
Subscribe now!
The Latest
Newsletter
Tech Tip Videos
- Jul-01-09
- Jun-29-09
Recently Popular
From the Magazine
July 2009, #183
News Flash: Linux Kernel 3.0 to include an on-the-go Expresso machine interface! Ok, maybe not, but Linux is definitely going mobile, from phones to e-readers. Find out more inside about Android, the Kindle 2, the Western Digital MyBook II, The Bug, and Indamixx (a portable recording studio). And if you've gone mobile and you been wanting more Emacs in your life then check out Conkeror.
To compliment the mobile we've got the stationary: parsing command line options with getopt, checking your Ruby code with metric_fu, and building a secure Squid proxy. How is this stationary you ask? What can we say? It's not. We just wanted to see if anybody actually read this part of the page :) .
All this and more, and all you have to do is get your hot sweaty hands on the latest copy of Linux Journal.
Delicious
Digg
StumbleUpon
Reddit
Facebook








increment
On April 15th, 2009 Anonymous (not verified) says:
Hi,
Is it possible to specify an increment in the brace expansion?
No increment
On April 16th, 2009 Mitch Frazier says:
You mean something like:
a{0..4:2}b => a0b a2b a4bIf so, the answer is no.
__________________________Mitch Frazier is an Associate Editor for Linux Journal and the Web Editor for linuxjournal.com.
The second example is not
On May 30th, 2008 Anonymous (not verified) says:
The second example is not quite right:
a{0..3}b => a0b a1b a2b a3b
Oops
On May 31st, 2008 Mitch Frazier says:
Typo, thanks, I'll fix it.
__________________________Mitch Frazier is an Associate Editor for Linux Journal and the Web Editor for linuxjournal.com.
Post new comment