Bash Brace Expansion

May 30th, 2008 by Mitch Frazier in

Your rating: None Average: 5 (8 votes)

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
A counted for loop using bash sequences requires the least amount of typing:
for i in {0..19}
do
    echo $i
done
But beyond counted for loops, brace expansion is the only way to create a loop with non-numeric "indexes":
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/bar
You 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.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Samuel Huckins's picture

Seems to not work for the "host" command

On October 20th, 2009 Samuel Huckins (not verified) says:

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?

Mitch Frazier's picture

It works but it doesn´t...

On October 21st, 2009 Mitch Frazier says:

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:

  host www1.yahoo.com www2.yahoo.com

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 and the Web Editor for linuxjournal.com.

Philippe Petrinko's picture

New feature for increment in Bash V4

On July 16th, 2009 Philippe Petrinko (not verified) says:

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)

Mitch Frazier's picture

I Agree That They So

On July 16th, 2009 Mitch Frazier says:

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 and the Web Editor for linuxjournal.com.

Anonymous's picture

increment

On April 15th, 2009 Anonymous (not verified) says:

Hi,

Is it possible to specify an increment in the brace expansion?

Mitch Frazier's picture

No increment

On April 16th, 2009 Mitch Frazier says:

You mean something like:

   a{0..4:2}b  => a0b a2b a4b

If so, the answer is no.

__________________________

Mitch Frazier is an Associate Editor for Linux Journal and the Web Editor for linuxjournal.com.

Anonymous's picture

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

Mitch Frazier's picture

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

Please note that comments may not appear immediately, so there is no need to repost your comment.
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <pre> <ul> <ol> <li> <dl> <dt> <dd> <i> <b>
  • Lines and paragraphs break automatically.

More information about formatting options

Newsletter

Each week Linux Journal editors will tell you what's hot in the world of Linux. You will receive late breaking news, technical tips and tricks, and links to in-depth stories featured on www.linuxjournal.com.
Sign up for our Email Newsletter

Tech Tip Videos

From the Magazine

December 2009, #188

If last month's Infrastrucuture issue was too "big" for you then try on this month's Embedded issue. Find out how to use Player for programming mobile robots, build a humidity controller for your root cellar, find out how to reduce the boot time of your embedded system, and if you're new to embedded systems find out the basics that go into one. You can also read about the Beagle Board, the Mesh Potato and a spate of other interestingly named items. And along with our regular columns don't miss our new monthly column: Economy Size Geek.


Read this issue