My Favorite bash Tips and Tricks
which produces the same result. However, brace expansion becomes quite useful when the brace-enclosed list occurs immediately before, after or inside another string:
$ echo {one,two,red,blue}fish onefish twofish redfish bluefish $ echo fish{one,two,red,blue} fishone fishtwo fishred fishblue $ echo fi{one,two,red,blue}sh fionesh fitwosh firedsh fiblueshNotice that there are no spaces inside the brackets or between the brackets and the adjoining strings. If you include spaces, it breaks things:
$ echo {one, two, red, blue }fish {one, two, red, blue }fish $ echo "{one,two,red,blue} fish" {one,two,red,blue} fishHowever, you can use spaces if they're enclosed in quotes outside the braces or within an item in the comma-separated list:
$ echo {"one ","two ","red ","blue "}fish one fish two fish red fish blue fish $ echo {one,two,red,blue}" fish" one fish two fish red fish blue fishYou also can nest braces, but you must use some caution here too:
$ echo {{1,2,3},1,2,3} 1 2 3 1 2 3 $ echo {{1,2,3}1,2,3} 11 21 31 2 3Now, after all these examples, you might be thinking to yourself, “Gee, those are great parlor tricks, but why should I care about brace expansion?”
Brace expansion becomes useful when you need to make a backup of a file. This is why it's my favorite shell trick. I use it almost every day when I need to make a backup of a config file before changing it. For example, if I'm making a change to my Apache configuration, I can do the following and save some typing:
$ cp /etc/httpd/conf/httpd.conf{,.bak}Notice that there is no character between the opening brace and the first comma. It's perfectly acceptable to do this and is useful when adding characters to an existing filename or when one argument is a substring of the other. Then, if I need to see what changes I made later in the day, I use the diff command and reverse the order of the strings inside the braces:
$ diff /etc/httpd/conf/httpd.conf{.bak,} 1050a1051 > # I added this comment earlier
Brace expansion is pretty cool when used as pseudo-globbing.


