Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. Bash Dictionary
  3. {} (Brace Expansion)

{} (Brace Expansion)

Brace expansion ({}) generates a list of strings from comma-separated values or a sequence of numbers and characters in one step. It significantly reduces repetition in bulk file creation, copying, and loops.

Syntax

Expand comma-separated values.

{a,b,c}

Sequence of integers.

{start..end}
{start..end..step}

Sequence of characters.

{a..z}
{A..Z}

Combine with a prefix and/or suffix.

prefix{a,b,c}suffix

Nested braces are also supported.

{a,{b,c},d}

Expansion Patterns

ExpansionExample resultDescription
{a,b,c}a b cExpands comma-separated strings.
{1..5}1 2 3 4 5Expands a sequence of integers.
{1..10..2}1 3 5 7 9Expands integers with a step value.
{a..e}a b c d eExpands a sequence of alphabet characters.
file{1..3}.txtfile1.txt file2.txt file3.txtExpands with a prefix and suffix attached.
{01..05}01 02 03 04 05Expands with zero-padding.
{a,b}{1,2}a1 a2 b1 b2Combines multiple braces to generate all combinations.

Sample Code

Basic comma expansion.

echo {apple,banana,cherry}
apple banana cherry

Expanding a sequence of integers.

echo {1..5}
1 2 3 4 5

Expanding with a step value.

echo {0..20..5}
0 5 10 15 20

You can also expand in reverse order.

echo {5..1}
5 4 3 2 1

Expanding a sequence of alphabet characters.

echo {a..f}
a b c d e f

Useful for creating multiple files or directories at once.

touch report_{2023,2024,2025}.txt
ls report_*.txt
report_2023.txt  report_2024.txt  report_2025.txt
mkdir -p project/{src,tests,docs,build}
ls project/
build  docs  src  tests

The {,.bak} expansion trick creates a backup of a file. Because the first element is an empty string, this is equivalent to cp config.json config.json.bak.

cp config.json{,.bak}

Expanding with zero-padding.

echo {01..05}
01 02 03 04 05

Combining with a for loop.

deploy.sh
for env in {dev,staging,prod}; do
    echo "Deploying to: $env"
done
bash deploy.sh
Deploying to: dev
Deploying to: staging
Deploying to: prod

You can also enter a for loop directly in the terminal. After pressing Enter after do, a > prompt appears — this means the shell is waiting for more input. Enter done to execute.

for env in {dev,staging,prod}; do
    echo "Deploying to: $env"
done
Deploying to: dev
Deploying to: staging
Deploying to: prod

Combining multiple braces generates all combinations (the Cartesian product).

echo {A,B}{1,2,3}
A1 A2 A3 B1 B2 B3

Notes

Brace expansion is processed first in the shell's expansion order. Keep this in mind when using variables inside braces — for example, {1..$n} does not work, because variables are expanded after brace expansion. To use a variable as a range, use the $ seq command or a C-style loop inside a for loop.

The zero-width expansion trick, such as cp file{,.bak}, is handy for creating file backups. It works because the first element of {,} is an empty string.

If you find any errors or copyright issues, please .