Advanced Bash Scripting
Expansions Part Two
Brace
Brace expansion is a powerful shell feature that generates arbitrary strings or arguments by evaluating expressions within curly braces {}
. Unlike globs, which match existing filenames, brace expansions create new text before any other expansion (such as pathname or parameter expansion) takes place.
Note
Brace expansions are always processed before globs and parameter expansions. This lets you quickly generate lists of filenames, parameters, or other strings in a single command.
Table of Content
- Basic Range Expansion
- Department Usernames Example
- Comma-Separated List Expansion
- Numeric Range Expansion
- Nested Brace Expansions
- Step-Based Range Expansion (Bash 4.0+)
- Prefix and Suffix with Brace Expansion
- Integrating Brace Expansion in Scripts
- Summary & Best Practices
- Links and References
1. Basic Range Expansion
Alphabetic and numeric ranges let you generate sequences with minimal syntax.
# Alphabetic sequence: a, b, c
touch sample{a..c}
ls
# Numeric sequence: 1, 2, 3
touch sample{1..3}
ls
# Descending numeric sequence
echo {3..1}
# → 3 2 1
Expansion Type | Syntax | Result |
---|---|---|
Alphabetic Range | {a..z} | a b c ... z |
Numeric Range | {1..5} | 1 2 3 4 5 |
2. Department Usernames Example
Suppose you need to create usernames for Marketing (MKT), Sales (SL), and Development (DEV), each with a three-digit suffix from 001
to 004
. Brace expansion handles this in one command:
touch {MKT,SL,DEV}{001..004}
ls
# → DEV001 DEV002 DEV003 DEV004 MKT001 MKT002 MKT003 MKT004 SL001 SL002 SL003 SL004
{MKT,SL,DEV}
expands to each department code.{001..004}
produces four numeric suffixes.- Combined, you get all 12 unique usernames in one step.
3. Comma-Separated List Expansion
Use commas for arbitrary lists of items. Each element is substituted in place of the braces.
echo file{alpha,beta,gamma}.txt
# → filealpha.txt filebeta.txt filegamma.txt
4. Numeric Range Expansion
Generate a series of numeric filenames quickly:
echo file{1..5}.txt
# → file1.txt file2.txt file3.txt file4.txt file5.txt
5. Nested Brace Expansions
Nest multiple sets of braces to produce Cartesian products:
echo {a,b}{1,2,3}
# → a1 a2 a3 b1 b2 b3
The shell pairs each element of the first set (a
, b
) with each element of the second set (1
, 2
, 3
).
6. Step-Based Range Expansion (Bash 4.0+)
Include a step value to skip elements in a numeric range. Requires Bash 4.0 or newer.
Warning
Step-based expansions ({start..end..step}
) are supported only in Bash 4.0+ and some other modern shells. Verify your shell version with bash --version
.
echo file{1..10..2}.txt
# → file1.txt file3.txt file5.txt file7.txt file9.txt
7. Prefix and Suffix with Brace Expansion
Wrap fixed text around your expansions:
echo pre-{A..C}-post
# → pre-A-post pre-B-post pre-C-post
8. Integrating Brace Expansion in Scripts
Brace expansions work seamlessly within loops, conditionals, and functions:
#!/usr/bin/env bash
for i in {1..3}; do
touch "report_${i}.log"
done
ls
# → report_1.log report_2.log report_3.log
Combine expansions with other shell features to automate repetitive tasks.
9. Summary & Best Practices
- Brace expansions run before all other shell expansions, making them ideal for generating arguments.
- Use numeric ranges for file series or versioned assets.
- Leverage nested braces for multi-dimensional data generation.
- Remember that stepped ranges require Bash 4.0+.
By mastering brace expansion, you can cut down on repetitive typing, automate bulk operations, and keep your scripts concise and readable.
10. Links and References
- Bash Reference Manual: Shell Expansions
- Advanced Bash-Scripting Guide
- Stack Overflow: Brace Expansion Examples
Watch Video
Watch video content