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 the System Administrator at Linux Journal.
Special Magazine Offer -- 2 Free Trial Issues!
Receive 2 free trial issues of Linux Journal as well as instant online access to current and past issues. There's NO RISK and NO OBLIGATION to buy. 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.
Sorry, offer available in the US only. International orders, click here.
Subscribe now!
The Latest
Featured Videos
Linux Journal Live - Oct 2, 2008
October 3rd, 2008 by Shawn Powers
The October 2, 2008 edition of Linux Journal Live! Associate Editor, Shawn Powers, and Steven Evatt, Online Development manager for The Houston Chronicle discuss surviving disaster with Linux.
Mastering IPTables, Part I
October 2nd, 2008 by Elliot Isaacson
Linux comes with a powerful firewall built-in, although the interface can be a little intimidating. This is the first in a multi-part tutorial on how to master basic and not-so-basic IPTables functionality and create the perfect firewall for your home network.
Recently Popular
From the Magazine
November 2008, #175
There aren't many numbers that put the US national debt to shame, but here's one: 1,100,000,000,000,000. What's that? That's how many floating-point operations per second the Roadrunner supercomputer at Las Alamos can perform. That's about 100 FLOPS per dollar of US debt (unfortunately, the debt is winning the second derivative race). Read the article about Roadrunner in this month's High Performance Computing issue of LJ.
Along with that, find out how to program the Cell processor and how to use CUDA with your NVIDIA GPU. Also in this issue: Mr HandS (aka Kyle Rankin) gives us a few tips on using Compiz, Chef Marcel shows you how to get blogging off your plate quicker, Mick Bauer talks about Samba security, Dan Sawyer interviews Cory Doctrow and Doc talks about how information technology can affect democracy and fix the national debt (just kidding about that last part). That and more for your reading pleasure in this month's Linux Journal.
Delicious
Digg
Reddit
Newsvine
Technorati








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 the System Administrator at Linux Journal.
Post new comment