Americas

  • United States
sandra_henrystocker
Unix Dweeb

Chomping down on bash’s brace expansion

How-To
Jun 23, 20165 mins
Data CenterLinux

Brace expansion is one of the interesting features of bash that many users are barely aware of while many others claim is one of their favorite features.

The use of braces — { and } — for expanding an expression on the command line can save you a lot of time when you want to generate a lot of commands or strings with very little effort. But you have to first get comfortable with how it works.

One of the simplest examples of brace expansion would be something like this. Say I want to generate a list of numbers that starts with 1 and goes up to my very favorite number — 111. Given brace expansion, this task is incredibly easy.

$ echo {1..111}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
109 110 111

The expression 1..111 is easy to recognize as representing a range of numbers. But that’s just a start. You can make the number sequence go backwards as easily as you can make it go forward.

$ echo {111..1}
111 110 109 108 107 106 105 104 103 102 101 100 99 98
97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80
79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62
61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44
43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26
25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7
6 5 4 3 2 1

In addition, you can work with characters as easily as you can work with numbers.

$ echo {a..z}
a b c d e f g h i j k l m n o p q r s t u v w x y z
$ echo {z..a}
z y x w v u t s r q p o n m l k j i h g f e d c b a

Just don’t try it with multi-character strings. Invalid range expressions will simply be treated as strings.

$ echo {aa..zz}
{aa..zz}

You can, however, get the effect you were probably looking for with an expression like this.

$ echo {a..e}{a..e}
aa ab ac ad ae ba bb bc bd be ca cb cc cd ce da db dc dd de ea eb ec ed ee

The “..” in ll of these expressions like 1..5 and a..z obviously indicates that the contents of the braces should be treated as a range.

$ echo {3..6}
3 4 5 6
$ echo {9..6}
9 8 7 6

You can also use brace expressions to step through a specific series of numbers, though there is clearly little advantage in doing something as simple as what I’ve shown below. You could type “echo 1 2 3 4 5” with a bit less effort.

$ echo {1,2,3,4,5}
1 2 3 4 5

One the other hand, when stepping through the numbers is only part of what you want to do, you might save yourself a little trouble.

$ echo step{1,2,3,4,5}
step1 step2 step3 step4 step5

And this is even less work.

$ echo step{1..5}
step1 step2 step3 step4 step5

You can also use brace expressions to simplify some common tasks. In the command below, we are backing up our Apache configuration file. What the brace expression is doing in this example is turning httpd.conf{,.bak} into “httpd.conf httpd.conf.bak”.

$ cp httpd.conf{,.bak}

The comma within the expression works like the commas shown in the step{1,2,3,4,5} expression above. Each time through loop, a separate element in the list is used. However, in the backup command, the first element in the expression is empty, so nothing is appended. You can test the expression using echo like this:

$ echo httpd.conf{,.bak}
httpd.conf httpd.conf.bak

If you want to do the reverse (i.e., recover httpd.conf.bak as httpd.conf), you would only have to move the location on the comma in the command. And here we’re using mv instead of cp.

$ mv http.conf{.bak,}

Again, you can test the expression with an echo command to verify that this is indeed what it does.

$ echo http.conf{.bak,}
http.conf.bak http.conf

You can also use brace expansion to compare the two files.

diff httpd.conf{,.bak}

You can use multiple brace expansions in a single line of code. Note how, in the example below, we go through A1 through A11, B1 through B11 and so on.

$ echo {A..E}{1..11}
A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 B1 B2 B3 B4 B5 B6 B7 B8 B9 B10 B11 C1 C2
C3 C4 C5 C6 C7 C8 C9 C10 C11 D1 D2 D3 D4 D5 D6 D7 D8 D9 D10 D11 E1 E2 E3 E4
E5 E6 E7 E8 E9 E10 E11

In the expression below, from a book by Dr Seuss, blanks are included in the output phrases by including them within double quotes.

$ echo {"one ","two ","red ","blue "}fish
one fish two fish red fish blue fish

If you want to set up a series of directories and subdirectories for the four accounts listed.

# cd /home
# mkdir -p {bfox,jdoe,shs,tuser}/{code,backups,images}
mkdir -p bfox/code bfox/backups bfox/images jdoe/code jdoe/backups jdoe/images shs/code shs/backups shs/images tuser/code tuser/backups tuser/images

This command would give you the large series of new directories listed below.

# find . -type d -print
.
./shs
./shs/images
./shs/images/production
./shs/images/test
./shs/backups
./shs/backups/production
./shs/backups/test
./shs/code
./shs/code/production
./shs/code/test
./bfox
./bfox/images
./bfox/images/production
./bfox/images/test
./bfox/backups
./bfox/backups/production
./bfox/backups/test
./bfox/code
./bfox/code/production
./bfox/code/test
./jdoe
./jdoe/images
./jdoe/images/production
./jdoe/images/test
./jdoe/backups
./jdoe/backups/production
./jdoe/backups/test
./jdoe/code
./jdoe/code/production
./jdoe/code/test
./tuser
./tuser/images
./tuser/images/production
./tuser/images/test
./tuser/backups
./tuser/backups/production
./tuser/backups/test
./tuser/code
./tuser/code/production
./tuser/code/test

Brace expansion can be very handy when you want to use otherwise very tedious commands. And, as I said, for some Unix users, it’s one of the shell’s best features.

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.

More from this author