Curly brace expansion in the Bash shell

A very handy trick using the Bash shell in Linux involves the curly braces. The Bash manual at gnu.org defines brace expansion as "a mechanism by which arbitrary strings may be generated."

I use this mostly when working with files, especially making back ups of configuration files. For example, to copy /etc/X11/xorg.conf to /etc/X11/xorg.conf.backup, you could do this:

$ cp /etc/X11/xorg.conf /etc/X11/xorg.conf.backup

or you could use brace expansion

$ cp /etc/X11/xorg.{conf,conf.backup}

which produces the same result. An easy way to visualize this is using the following example:


jacob@jakes-laptop:~$ echo a{r,l,cm}e
are ale acme

Comments

Hi Jacob,

Interesting tip you explain here. Let me add to your cp example:

% cp /etc/X11/xorg.{conf,conf.backup}

Since the very reason for using brace expansion is to shorten the command by avoiding typing common things many times, you could as well have written:

% cp /etc/X11/xorg.conf{,.backup}

The "conf" part is also common, so you can take it out of the brace, and take advantage of the fact that empty strings are valid inside the brace. If that was not the case:

% cp /etc/X11/xorg.con{f,f.backup}

Use no spaces in the curly braces or it breaks:

OK:
% echo {Alessya,Tanya,Stephen}
Alessya Tanya Stephen

NOT OK:
% echo {Alessya,Jean Luke,Stephen}
{Alessya,Jean Luke,Stephen}

If you really need a space, quote them
ALSO OK:
% echo {Alessya,"Jean Luke",Stephen}
Alessya Jean Luke Stephen

It is a really well-taken decision, as I believe that it should be blocked from the day one in the schools or libraries. Because in this way students are involved on bestessay.com review so many other things as compared just to focus on their studies. So this is a really right decision taken by the management.