Brace expansion is a powerful feature in the Bash shell that allows users to generate sequences of text based on specified patterns. It provides a concise and efficient way to create multiple similar entries without the need for explicit enumeration. In this article, we will explore various use cases of brace expansion and learn how to leverage it effectively in different scenarios.
This article is part of my series on bash. To have introductory knowledge of bash, have a look at this article – Useful Bash Commands in Linux with Examples.
Table of Contents
1. Basic Brace Expansion
The simplest form of brace expansion generates a sequence of numbers. For example:
[ajay@legion ~]$ echo {1..5}
1 2 3 4 5
This command generates the sequence from 1 to 5 and prints it on the screen. It’s useful when you want to create a range of values quickly and efficiently.
Using Echo Command in Linux/Unix is one of the most widely used commands used with bash. So please have a look at it.
2. Brace Expansion with Leading Zeros
Brace expansion can handle leading zeros as well:
[ajay@legion ~]$ echo {01..10}
01 02 03 04 05 06 07 08 09 10
Here, the sequence goes from 01 to 10, preserving the leading zeros. This feature is often used when dealing with filenames that have a fixed pattern with leading zeros.
3. Brace Expansion with Letters
Brace expansion is not limited to numbers; it can also handle letters:
[ajay@legion ~]$ echo {a..e}
a b c d e
This command generates the sequence of lowercase letters from ‘a’ to ‘e’. It can be handy when you need to create lists of letters or iterate over characters in a specific range.
4. Combining Letters and Numbers using Brace Expansion
You can combine multiple brace expansions:
[ajay@legion ~]$ echo {a..d}{1..3}
a1 a2 a3 b1 b2 b3 c1 c2 c3 d1 d2 d3
Here, it generates combinations of letters ‘a’ to ‘d’ with numbers ‘1’ to ‘3’. This can be useful for generating combinations of variables or iterating over a 2D grid of values.
5. Nested Brace Expansion
Brace expansion can be nested as well:
[ajay@legion ~]$ echo {{1..3},{a..c}}
1 2 3 a b c
This command generates a combined sequence of numbers from 1 to 3 and lowercase letters from ‘a’ to ‘c’. Nesting brace expansions provides a way to create complex patterns easily.
6. Incremental Brace Expansion
You can specify a step value to create incremental sequences:
[ajay@legion ~]$ echo {1..10..2}
1 3 5 7 9
This generates the sequence from 1 to 10 with a step of 2. It’s particularly useful when you need to create sequences with a specific increment, such as odd or even numbers.
7. Character Range Brace Expansion
Brace expansion can also work with character ranges:
[ajay@legion ~]$ echo {D..I}
D E F G H I
It generates the uppercase alphabet from ‘D’ to ‘I’. This feature is especially useful for creating patterns involving alphabetic characters.
8. Using Brace Expansion in a Command
Brace expansion can be very useful when combined with other shell commands*. For example:
[ajay@legion ~]$ touch file_{1..5}.txt
[ajay@legion ~]$ ls
file_1.txt file_2.txt file_3.txt file_4.txt file_5.txt
In this example, we used brace expansion to create five files named file_1.txt
to file_5.txt
using the touch
command. This technique can save time and effort when you need to create a batch of similar files quickly.
Look at The touch Command in Linux/Unix with Examples to learn more about it.
Learn more about shell scripting in this article.
9. Using Brace Expansion to Create Directories
Brace expansion is also handy for creating multiple directories in one go:
[ajay@legion ~]$ mkdir {apple,banana,orange}_fruit
[ajay@legion ~]$ ls
apple_fruit banana_fruit orange_fruit
This command creates three directories named apple_fruit
, banana_fruit
, and orange_fruit
. It’s a convenient way to create multiple directories with a common pattern in their names.
10. Brace Expansion with Variables
Brace expansion can work with variables as well:
[ajay@legion ~]$ var1=hello
[ajay@legion ~]$ var2=world
[ajay@legion ~]$ echo "{$var1,$var2}"
{hello,world}
Here, the brace expansion does not evaluate the variables but treats them as text. This can be useful when you want to use brace expansion as part of a larger command that involves variables.
11. Brace Expansion with Special Characters
Brace expansion can handle special characters too:
[ajay@legion ~]$ echo {.,_,-}
. _ -
This command generates a sequence containing a dot, underscore, and hyphen. It’s helpful for quickly generating lists of special characters.
12. Using command output with brash expansion
You can use command output within brace expansion:
[ajay@legion ~]$ date +%d
03
[ajay@legion ~]$ echo {01..$(date +%d)}
01 02 03
This example uses the date command to obtain the current day of the month, represented in two-digit format (which is 03
). It then utilizes brace expansion to create a sequence of numbers from 01 to the obtained day of the month hence 01 02 03
is the output.
brace expansion combines the output of two brace expansions
with numbers and letters. It can be used to generate complex combinations of strings.
Brace expansion is a versatile feature that simplifies working with sequences and repetitive tasks in the Bash shell. It can significantly reduce the amount of manual effort needed to generate lists of data. Whether you need to create directories, files, or even complex combinations of characters, brace expansion is a powerful tool at your disposal.
For more commands (with examples) used in association with Bash, check out these informative articles:
- Regular Expression (Regex) and Regexp in Linux
- The Copy Command (cp) in Linux/Unix
- The grep command in Linux
These articles cover a wide range of topics related to Linux, Bash, and other useful commands and tools. Whether you are a beginner or an experienced user, these resources will help you improve your Linux skills and make you more proficient in working with the command-line interface. And also don’t forget to check out official GNU guide on brace expansion in bash.
Remember, mastering Bash and Linux can greatly improve your productivity and efficiency in various tasks. So keep exploring, experimenting, and learning new tricks to become a proficient Linux user. Happy coding!
Pingback: How to Use Loops in Bash? | SmartTech101