Linux Command Reference ยท Bash Syntax
Bash brace expansion
Reference for Bash brace expansion: comma lists, numeric and letter ranges, steps, zero padding, nesting and prefixes, with the order of expansion and pitfalls.
Brace expansion generates several words from one pattern. file{1,2,3}.txt becomes file1.txt file2.txt file3.txt, and {1..5} becomes 1 2 3 4 5. Unlike a glob, it does not look at the file system: the words are produced whether or not any file exists.
It is one of the first expansions Bash performs, before variables and globs, which explains both its power and its limits.
mkdir -p project/{src,docs,tests} echo {a..e} cp config.yml{,.bak}
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
prefix{item1,item2,...}suffix
{start..end}
{start..end..step}
| Part | Description |
|---|---|
{a,b,c} |
A comma list of items. Items may be empty and may contain nested braces. |
{x..y} |
A sequence of integers or single letters from x to y, ascending or descending. |
{x..y..n} |
The same, advancing by n. The sign of the step is ignored; direction comes from the endpoints. |
{01..10} |
If either endpoint starts with a zero, all numbers are padded to the same width. |
| prefix, suffix | Text before and after the braces is attached to every generated word. Several brace groups multiply (a Cartesian product), leftmost varying slowest. |
Returns: the generated words in order, as separate words. Edge cases: braces containing no unquoted comma or valid sequence are left as they are, and unmatched braces are literal.
Usage notes
- No variables in ranges.
{1..$n}does not work because brace expansion runs before parameter expansion. Useseq, a C-stylefor ((i=1; i<=n; i++))loop, orevalwith great care. - It is not a glob.
{a,b}always expands, even ifaandbdo not exist as files. Use globbing to match existing files. - Quotes disable it. Inside single or double quotes, the braces are literal. Quote deliberately when you pass a pattern to another tool such as
find -name. - Nice idioms:
cp file{,.bak}(backup),mv name.{txt,md}(rename extension),mkdir -p a/{b,c}/{d,e}(tree),rm -r dir/{a,b}. - Very large ranges such as
{1..1000000}build the entire word list in memory. Use a loop instead. - Not in POSIX
sh.dashandshdo not know brace expansion; use#!/usr/bin/env bashwhen scripts rely on it. - Letter ranges use character codes, so a range that mixes cases (
{a..Z}) also produces the punctuation characters between them; stay within one case. - Count sanity check: the number of words is the product of the sizes of all groups, so two groups of 10 produce 100 words.
- Brace expansion after other text works anywhere in a word:
/etc/{passwd,group}expands to two full paths.
Related
- Globbing, Tilde expansion, Quoting,
seq,mkdir - Lessons: Globbing, Expansion order, Loops