Bash brace expansion

29/12/2020
Bash uses brace expansion to generate a sequence of strings from the terminal or by using any bash script. A sequence of expressions or comma-separated list of data with curly brackets is used to define brace expansion. Two optional parts of brace expansion are preamble and postscript. The preamble is used to add text at the front of each generated string and postscript is used to append text at the end of the generated string by using brace expansion. How the user can create different types of random strings using bash brace expansion is explained in this tutorial by using various examples.

Syntax:

  1. String List
{String1, String2,… ,StringN }
  1. Range List
{<start> . . <end>}
  1. Preamble and postscript
<preamble>{ string or range }
{ 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 {PHP,Javascript,JQuery}
$ 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 {50..60}
$ 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 "Hi "{John, Mohammed, Lisa}
$ 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 {John,Mohammed,Lisa}" is a programmer."
$ 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 "Hi, "{John,Mohammed,Lisa}" welcome to LinuxHint."
$ 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.

$ mkdir {Design,Programming,Framework}
$ 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.

$ touchps{1..3}”.py”
$ 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.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Bash Script User Input

Taking input from the user is a common task for any programming language. You can take input from a user in bash script...
28/12/2020

Tổng hợp 15 lệnh OpenVZ commands thông dụng

1, Command to list the running VPSs in a node # vzlist Example: # vzlist CTID NPROC STATUS IP_ADDR...
20/09/2021

How to use inotify and rsync to create a live backup system using only a while loop in a bash script

Why should you use Bash Scripts to perform folder synchronizations and backups? Bash is by far the most popular and used...
28/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Dịch vụ thuê mua proxy US UK uy tín, chất lượng số #1
13/05/2024

Thuê mua proxy Việt Nam: Báo giá & các thông tin MỚI NHẤT
13/05/2024

Dịch vụ thuê mua proxy giá rẻ an toàn, tốc độ cao
13/05/2024

Thuê mua proxy V6 uy tín, chất lượng tại đâu?
11/05/2024

Thuê mua proxy Tiktok tăng doanh thu, hiệu quả cao
11/05/2024