Syntax:
- String List
- Range List
- Preamble and postscript
{ string or range }<postscript>
<preamble{ string or range }<postscript>
The above syntax shows that you can use brace expansion without preamble and postscript or with preamble or with postscript or with both. Different uses of brace expansions are shown in the next part of this tutorial by using various examples.
Example-1: Using comma-separated lists
The following command shows the use of brace expansion with comma-separated list only. You have to remember one thing while defining the list. That is, list items must be separated by comma only and don’t add any space between the items, otherwise brace expansion will not work. Here, the first command will display the list items with space. Two lists are used in the second command and the output will generate by combining each items of each list.
$ echo {"I like ","Learn "}{"PHP","Programming"}
Output:
The following output will appear after running the script. In the second command, there are two items in each list and there are two lists. So, the second command will print (2X2=4), four text as output.
Example-2: Using Ranges
Different types of ranges can be used in brace expansion to generate the list of data. The uses of four types of ranges are shown in this example. The first range will create a list of numeric data, starting from 50 to 60. The second range will generate a list of alphabetic characters, starting from A to F. The third range will generate a list of number, starting from 1 to 5 with leading zero. The forth range will generate a list of alpha-numeric data by combining A to C and 1 to 3.
$ echo {A..E}
$ echo {01..05}
$ echo {A..C}{1..3}
Output:
The following output will appear after running the above commands. In the last command, the first brace expansion contains three items and the second brace expansion contains three items. So, the total items in the output will be, 3X3 = 9.
Example-3: Using preamble
This example shows the use of preamble in brace expansion. The first command will add the string, “Hi “ with each item of the list and generate the output. The second command will add ‘b’ with each item of the list. This type of task is beneficial if you want to add a common text or character at the front of each list item.
$ echo b{all, ell, oat, eef}
Output:
The following output will appear after running the commands.
Example-4: Using postscript
This example shows the use of postscript in brace expansion. The text, “ is a programmer” will add at the end of each list item in the first command. The word, “ball” will add with each item of the list in the second command. If the last part of each item of the list are same then it is better to create the list by brace expansion with postscript.
$ echo {basket,foot,volley}ball
Output:
The following output will appear after running the commands.
Example-5: Using both preamble and postscript
When the first part and last part of each item of the list are same then it better to create the list by using brace expansion with preamble and postscript. Here, the first command will add “Hi “, at the beginning of each list item and “ welcome to LinuxHint.“, at the end of the each list item. The second command will generate an alpha-numeric list by adding “*****” at the front and “.*****” at the end of the list item. According to the range, the first item is Q01 and the last item is Q05.
$ echo "*****Q"{01..05}".*****"
Output:
The following output will appear after running the commands.
Example-6: Creating sequence of directory and file
`echo` command is used in all previous examples of this tutorial. But you can use brace expansion with other commands also. How you can create multiple files or folders in a single command by using brace expansion is shown in this tutorial. The following command will create three folders, Design, Programming and Framework, by using `mkdir` command and brace expansion.
$ ls
Output:
The following output will appear after running the commands.
You can also create sequential multiple files by using touch and brace expansion with preamble and postscript. In this example, `touch` command is used to create multiple files. “ps” is used as preamble, “.py” is used as postscript and 1..3 range is used to create the sequential file names. The second command, ‘ls’ will show the files are created or not.
$ ls
Output:
The following output will appear after running the commands. Here, three files will be created. These are ps1.py, ps2.py and ps3.py.
Conclusion
Brace expansion is very useful for generating a list of sequential data or running any command on sequence of data. Some common uses of brace expansion are shown in this tutorial. Hope, the reader will be able to use brace expansion properly after practicing the examples of this tutorial.