# # Simple Bash Brace Expansion
APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Bash Brace Expansion

I've removed advertising from most of this site and will eventually clean up the few pages where it remains.

While not terribly expensive to maintain, this does cost me something. If I don't get enough donations to cover that expense, I will be shutting the site down in early 2020.

If you found something useful today, please consider a small donation.



Some material is very old and may be incorrect today

© April 2008 Anthony Lawrence

The new tricks Bash has picked up in 3.0 are exciting and useful, but simple brace expansion has been available for some time now, and yet we seldom see it used. I suppose that's because the need doesn't come up too often. The classic example from the Bash man page is:



mkdir /usr/local/src/bash/{old,new,dist,bugs}
 

which expands to

mkdir /usr/local/src/bash/old /usr/local/src/bash/new \
   /usr/local/src/bash/dist /usr/local/src/bash/bugs
 

The man page also gives a more complex example showing nested braces:

chown root /usr/{ucb/{ex,edit},lib/{ex?.?*,how_ex}}
 

That's a little harder to follow, but if you replace "chown root" with "echo" you can see it:

$ echo  /usr/{ucb/{ex,edit},lib/{ex?.?*,how_ex}}
/usr/ucb/ex /usr/ucb/edit /usr/lib/ex?.?* /usr/lib/how_ex
 

There are other possibities beyond nesting:

$ echo foo{1,2}{a,b}
foo1a foo1b foo2a foo2b
 

And did you know that you can leave out the arguments? As long as you have the comma, this works:

$ echo foo{,}{,}
foo foo foo foo
 

And so does this:

$ echo foo{,,,}
foo foo foo foo
 

Braces can get pretty complex:

$ echo foo{1,2,3,4}bar{7,8{foo,bar}}
foo1bar7 foo1bar8foo foo1bar8bar foo2bar7 foo2bar8foo foo2bar8bar foo3bar7 
foo3bar8foo foo3bar8bar foo4bar7 foo4bar8foo foo4bar8bar
# with 3.0 bash the "foo{1,2,3.4}" can be "foo{1..4}"
 

But when would you ever need anything like that?


If you found something useful today, please consider a small donation.



Got something to add? Send me email.





(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

->
-> Simple Bash Brace Expansion

4 comments


Inexpensive and informative Apple related e-books:

El Capitan: A Take Control Crash Course

Take Control of Upgrading to El Capitan

Take Control of Preview

Digital Sharing Crash Course

Take Control of Apple Mail, Third Edition





More Articles by © Anthony Lawrence







Mon Apr 21 22:38:23 2008: 4091   KenGregg


Cool. I guess that'll teach me to not RTFM cover to cover.

Back in my early days I worked with an os where the shell would expand what they referred to as an @list. Create a list of file names in a file and it would be expanded to the list with @filename syntax.

I have often wished I could do something like that in bash. I'll have to play with this.



Mon Apr 21 22:40:45 2008: 4092   TonyLawrence

gravatar
If you read everything cover to cover you'll never get anything else done :-)



Tue Apr 22 01:02:57 2008: 4097   badanov


Works in sh as well. Maybe bash got this working from the sh shell?



Tue Apr 22 13:05:11 2008: 4108   BigDumbDinosaur


mkdir -p <path> produces the (more or less) equivalent to bash's braces. However, a separate <path> has to be specified for each subdirectory that is desired, so there's a bit more typing involved.

------------------------


Printer Friendly Version

Have you tried Searching this site?

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us


Printer Friendly Version





The real problem is that programmers have spent far too much time worrying about efficiency in the wrong places and at the wrong times; premature optimization is the root of all evil (or at least most of it) in programming. (Donald Knuth)




Linux posts

Troubleshooting posts


This post tagged:

Basics

Linux

Scripting

Shell

Unix



Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode