Shell Brace Expansion

I would say that most developers that I know are comfortable with a shell/terminal but they are not proficient with the shell. I, for one, am constantly striving to improve my knowledge of the shell, customizing my environment, and trying to maximize my productivity with it. Even after years of constant usage I am always learning new tricks and techniques. It also doesn’t help that there are thousands of command line applications out there waiting to be absorbed.

My shell of choice, like most people I know, is bash. Most developers only use the basic features available in shells such as launching programs, command history, auto-completion, and the like. However, most shells have functionality people don’t even know about and could probably benefit from. Who knows, you might even be able to multitask and partake in foxy bingo online or pogo games while programming simultaneously. One such feature is Brace Expansion.

Take a look:

Brace Expansion

Its a little hard to see from the image so I’ll run down the examples here:

shell> echo hello{world,goodbye}
helloworld hellogoodbye

This example shows exactly what Brace Expansion is doing. Its taking the input line “hello{}” and replacing the “{}” section with each of the comma separated values, producing “helloworld” and “hellogoodbye.”

Its important to note that there is no space between “hello” and the opening brace. This is called the preamble and therefore it gets prepended to the front of both “world” and “goodbye.” Also, there is no space around the comma inside the braces. If there were a space then brace expansion doesn’t take place at all! The output itself will separate each expansion with a space, just like any other built-in shell expansion. If you really want spaces in the expansion portions you can escape them as you normally would and it will respect that.

shell> echo file.{txt,rb}
file.txt file.rb

This points out a couple things. First of all, I’m using echo to debug the brace expansion so you can see what it produced. What seemingly happens, like in the previous case, is that it ran the brace expansion when it parsed the input line, and turned it into the following:

shell> echo file.txt file.rb

Now some simple usages pop to mind. Need to rename a file? How about “mv filename.{txt,rb}.” Want to quickly backup a file, how about “cp filename.{txt,save}.” Whenever you feel yourself typing the same thing twice in a row that is a perfect candidate for brace expansion! Save yourself some keystrokes, some time, and potential errors by reusing what you’ve already typed in already!

The third usage is combining two separate but connected brace expansions “{}{}.” This works differently then two completely disjoint sets “{} … {}” and is still different then nested sets “{…{}…}.” Rather then discuss the semantics of those, which you can easily find out yourself by just trying them out, I’ll give you a glimpse of another trick you can do with brace expansion.

Here is a simple example of Sequences:

Brace Expansion Sequences

The syntax for sequences is “{#..#}.” You can even do simple character sequences like “{a..z}.” The second example from the picture shows a postscript, the opposite of the preamble. Its text that comes immediately after the braces (no space) and is thus appended to the end of each expansion.

I hope you give it try, you might end up liking it. I actually do some real neat tricks with it in one of my scripts. If you want to explore it further you should check out some of the more technical documentation:

Until next time, wrap your head around this crazy line. And no, there is no typo, its running a brace expansion once, which then outputs a very long command which includes brace expansion, and then runs that command with bash:

shell> echo echo {A..Z}\{0..{1..4}\} | bash

One Response


TheBonsai on June 9, 2009 at 4:53 pm  #

Well written article, congrats. I hope with things like this, newbies will see what shells really are made for.

Add a Comment