From the course: Bash Patterns and Regular Expressions

What is brace expansion?

- [Instructor] Brace expansion has been in the Bash shell for a very long time. It is not globing, but we can use it for pattern matching. There are a few things to know about brace expansion versus globs. Globs and extended globs, are pathname expansion, and expand the pathname. Brace expansion is just that, brace expansion, and only expands braces. You may recall in my video from chapter one about shell expansion order, that brace expansion is done first and pathname expansion is done last. Also, it's worth noting that brace expansion doesn't depend on the existence of files. Brace expansion is a string operation and we use it to create string. To demonstrate the difference I created a directory with 100,000 files in it. I used the ls command with the glob to list all files. I also used ls with brace expansion to list all files and lastly, I used echo with brace expansion to list all file names. In all three instances, I redirected output to slash dev slash null so I could focus on the speed of the operation itself and not the console output. The results were ls with the glob took 0.4 seconds, ls with brace expansion took 0.3 seconds, and echo with brace expansion took 0.1 seconds. Let me explain why this is. When ls wants to create a sorted list of files bash has to expand the glob based on files in the directory, ls needs to stat each file, ls needs to sort the list and then ls displays the list. In my second test I used ls again but used brace expansion to create the list of files to test. In this case, bash expands the braces to create the list in sorted order and then ls did a stat on each file and then ls displayed the list. This is faster, as the glob is not expanded based on the existence of files. Lastly, echo listed the file names using brace expansion. In this case, bash created the list of file names without statting any files and then echo displayed the list, which is why it's faster. Note that if you're not careful you may not see a speed increase. For example, typing ls, space, file, left curly bracket, one, dot, dot, 10, right curly bracket, asterisk, will match file1.txt, file2.txt, file3.txt, and so on. However, it's expanding to file1*, file2*, file3* first, via the very fast brace expansion, and then doing pathname expansion last by processing the glob. You can combine brace expansion and pathname expansion together, but just be aware of what's happening. Also, keep in mind that with brace expansion, the shell does not stat the files. In a terminal, be sure that you're in your chapter three directory of your exercise files. For me, my path is /home/user1/Exercise Files/Ch 03 Let's use brace expansion to create a few files. Type in touch, space, file, left curly bracket, one, comma, two, comma, four, right curly bracket, dot t-x-t, and hit enter. Type in ls. Now let's use brace expansion to list them. Type in ls, space, file, left curly bracket, one, dot, dot, four, right curly bracket, dot t-x-t, and hit enter. We get an error message saying that file3.txt does not exist. This is because ls did a stat on all four files. Now let's use echo. Type in echo, space, file, left curly bracket, one, dot, dot, four, right curly bracket, dot, t-x-t, and hit enter. It lists all four, and we don't get an error message because it doesn't make sure the file exists. Keep this in mind when using brace expansion. Properly formatted brace expansion has to have either one or more commas, or a proper range. We can also specify a comma that doesn't separate two items by leaving the field out. I'll get back to this in a moment. Type in clear. Then type in echo, space, left curly bracket, one, dot, dot, ten, right curly bracket. Very simply, a range looks like this: Left curly bracket, the start of the range, two dots, and the end of the range. Hit enter. We can create more complex combinations by combining the brace expansions. Type in echo, space, left curly bracket, a, dot, dot, c, right curly bracket, left curly bracket, 10, dot, dot, 39, right curly bracket, and hit enter. Or, you could type in echo, space, left curly bracket, a, comma, b, comma, c, right curly bracket, left curly bracket, d, comma, e, comma, f, right curly bracket, and hit enter. And you can see the combinations we've made. We can also specify an increment integer. We can specify an increment integer as well. Type in echo, space, left curly bracket, one, dot, dot, 100, dot, dot, two, right curly bracket and hit enter. This would create a list of odd numbers, such as one, three, and five, from one to 99. By starting from 2, it will create a list of all even numbers, such as two, four, and six, and so on. Type in echo, space, left curly bracket, two, dot, dot, 100, dot, dot, two, right curly bracket, and hit enter. For integers, we can also pad. Type in echo, space, left curly bracket, zero, zero, zero, two, dot, dot, 1000, dot, dot, two, right curly bracket, and hit enter. Now type in clear. We can do alphabetical ranges as well, but both the start and end letter need to be of the same type. It uses the locale of C: (See my video on the effect of locale on searches in chapter one for more information). Type in echo, space, left curly bracket, a, dot, dot, z, right curly bracket and hit enter. We can also skip letters. Bring your line back and add dot, dot, two, and hit enter again. We can also do reverse ranges. Type in echo, space, left curly bracket, 10, dot, dot, zero, right curly bracket, and hit enter. The last format I'll cover is a bit odd. Type in clear, then type in echo, space, file, left curly bracket, comma, dot, bak, right curly bracket and hit enter. This expands a string with a pattern before the comma, which is nothing, and then expands it with a pattern after the comma, which is .bak. This allows us an easy way to copy or rename files while saving time. For instance, type in echo, space, slash, this, slash, is, slash, a, slash, long, slash, path, slash, file, left curly bracket, comma, dot, bak, right curly bracket, and hit enter. And we have both the source and destination paths for copy or move.

Contents