Bash Brace Expansion
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.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- Designing Electronics with Linux
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Dynamic DNS—an Object Lesson in Problem Solving
- Using Salt Stack and Vagrant for Drupal Development
- New Products
- What's the tweeting protocol?
- A Topic for Discussion - Open Source Feature-Richness?
- Validate an E-Mail Address with PHP, the Right Way
- Drupal Is a Framework: Why Everyone Needs to Understand This
- Home, My Backup Data Center
- General
1 hour 33 min ago - Kernel Problem
11 hours 36 min ago - BASH script to log IPs on public web server
16 hours 3 min ago - DynDNS
19 hours 39 min ago - Reply to comment | Linux Journal
20 hours 11 min ago - All the articles you talked
22 hours 35 min ago - All the articles you talked
22 hours 38 min ago - All the articles you talked
22 hours 39 min ago - myip
1 day 3 hours ago - Keeping track of IP address
1 day 4 hours ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?



Comments
Seems to not work for the "host" command
I love brace expansion, but have found it doesn't work with the host command for some reason:
# Normal~: echo {1,2}
1 2
# Individual commands with host
~: host www2.yahoo.com
www2.yahoo.com is an alias for rc.yahoo.com.
rc.yahoo.com is an alias for rc.fy.b.yahoo.com.
rc.fy.b.yahoo.com has address 206.190.60.37
~: host www1.yahoo.com
www1.yahoo.com is an alias for rc.yahoo.com.
rc.yahoo.com is an alias for rc.fy.b.yahoo.com.
rc.fy.b.yahoo.com has address 206.190.60.37
# But as soon as I add brace expansion...
~: host www{1,2}.yahoo.com
;; connection timed out; no servers could be reached
Any ideas?
It works but it doesn´t...
Brace expansion is working, the problem is that host doesn't like the arguments you're giving it. Check the man page for host:
SYNOPSIS host [OPTIONS] {name} [server] DESCRIPTION ... name is the domain name that is to be looked up. ... server is an optional argument which is either the name or IP address of the name server that host should query instead of the server or servers listed in /etc/resolv.conf.So when you do:
you're telling host to use www2.yahoo.com as the DNS server. The crux is that host only looks up a single name per command invocation.
Mitch Frazier is an Associate Editor for Linux Journal.
New feature for increment in Bash V4
Hello Mitch,
Nice topic, thanks for your work, I enjoy LinuxJournal.
According to those 2 pages,
http://bash-hackers.org/wiki/doku.php/syntax/expansion/brace
http://www.gnu.org/software/bash/manual/bashref.html#Brace-Expansion
Increment has been added to the brace expansion in Bash v4.
Do you agree?
Philippe Petrinko (Versailles, France)
I Agree That They So
I have not used bash v4.0 so I can't confirm that the feature exists. I assume that they're not must making it up, although I will note that the news file, which according to the bash home page tersely lists the new features in bash-4.0, does not make any mention of the feature.
Mitch Frazier is an Associate Editor for Linux Journal.
increment
Hi,
Is it possible to specify an increment in the brace expansion?
No increment
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.
Possible in bash 4
In bash 4, you can specify an increment:
a{0..4..2}b =>a0b a2b a4b
The second example is not
The second example is not quite right:
a{0..3}b => a0b a1b a2b a3b
Oops
Typo, thanks, I'll fix it.
Mitch Frazier is an Associate Editor for Linux Journal.