APLawrence - Information and Resources for Unix and Linux Systems, Bloggers and the self-employed
RSS Feeds Get APLawrence.com by RSS











(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Home > Basics > Simple Bash Brace Expansion
Printer Friendly Version




Bash Brace Expansion



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 this page was useful to you, please click to help others find it:  

Your +1's can help friends, contacts, and others on the web find the best stuff when they search.

4 comments




More Articles by Anthony Lawrence - Find me on Google+



Click here to add your comments





Mon Apr 21 22:38:23 2008:   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:   TonyLawrence

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



Tue Apr 22 01:02:57 2008:   badanov
http://www.freefirezone.org

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



Tue Apr 22 13:05:11 2008:   BigDumbDinosaur
http://bcstechnology.net

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.

Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments


If you want a picture to show with your comment, go get a Gravatar



ad

Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

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. We appreciate comments and article submissions.

Publishing your articles here

Jump to Comments



Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.


My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!


book graphic unix and linux troubleshooting guide




 I sell and support
 Kerio Mail server
pavatar.jpg

This post tagged:

       - Basics
       - Linux
       - MacOSX
       - Scripting
       - Shell
       - Unix




Unix/Linux Consultants

Skills Tests

Guest Post Here