Brace expansion is a feature in bash (and other Unix shells) that allows you to generate arbitrary strings. It can be used for creating lists of strings, filenames, or paths efficiently. Here are the key points and examples to help you understand how brace expansion works:
The basic syntax of brace expansion is:
{item1,item2,item3}
This will expand into:
item1 item2 item3
echo {apple,banana,cherry}
Output:
apple banana cherry
echo file{1,2,3}.txt
Output:
file1.txt file2.txt file3.txt
mkdir -p project/{src,docs,tests}
This will create the following directories:
project/src
project/docs
project/tests
Braces can be nested to generate more complex patterns.
echo {A,B{1,2},C}
Output:
A B1 B2 C
You can use brace expansion to generate a sequence of numbers or letters.
echo {1..5}
Output:
1 2 3 4 5
echo {a..e}
Output:
a b c d e
You can combine lists and ranges for more complex expansions.
echo file{a..c}_{1..3}.txt
Output:
filea_1.txt filea_2.txt filea_3.txt fileb_1.txt fileb_2.txt fileb_3.txt filec_1.txt filec_2.txt filec_3.txt
touch file{1..3}.txt
This creates:
file1.txt file2.txt file3.txt
mkdir -p project/{src/{main,tests},docs,build}
This will create:
project/
├── build
├── docs
└── src
├── main
└── tests
echo pre{a,b,c}post
Output:
preapost prebpost precpost
mkdir -p backup/{2024-06-{01..30}}
This will create directories for each day in June 2024:
backup/
├── 2024-06-01
├── 2024-06-02
├── 2024-06-03
...
├── 2024-06-30
Brace expansion is a powerful tool in bash for generating multiple strings, filenames, or directory names from a pattern. It saves time and reduces errors compared to manually typing out each item. Remember:
{}
to define a list of items.{start..end}
for ranges.