Brace and tilde expansion

Beginner · 4 min

Generate lists and ranges with {a,b} and {1..5}, back up files with file{,.bak}, and know when ~ expands to a home directory and when it stays literal.

Before thisGlobbing and wildcardsQuoting - single, double and escapesParameter expansion

Script Bit · 19 min · from 0:24

Brace and tilde expansion

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Script BitReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Brace expansion writes out text: `{a,b,c}` becomes three words and `{1..5}` becomes five. It never looks at the file system, unlike a glob.
  • `file{,.bak}` expands to `file file.bak`, which makes `cp file{,.bak}` a one-line backup.
  • Braces are expanded before variables, so `{1..$n}` does not work. Use a `for ((...))` loop or `seq` when the bound is a variable.
  • An unquoted `~` at the start of a word expands to `$HOME`, and `~user` expands to that user's home. Quoted or mid-word, it stays a literal tilde.
  • Zero-padded ranges such as `{01..05}` and steps such as `{1..10..3}` need Bash 4. macOS's `/bin/bash` 3.2 ignores the padding and the step.

Typing less without guessing

Some jobs are repetitive in a predictable way: three directories with the same parent, a backup copy of a config, server1 through server5. You could type each name. Or you could describe the pattern and let Bash write the words.

Brace expansion does exactly that. It is text generation, performed before anything touches the file system. That is the difference from a glob like *.txt: a glob looks for files that exist, while braces invent words whether or not anything exists.

Lists, ranges and combinations

A comma list gives one word per item. Two dots give a range, and a third value sets a step:

$ echo {a,b,c}
a b c
$ echo {1..5} {a..e}
1 2 3 4 5 a b c d e
$ echo {5..1}
5 4 3 2 1
$ echo {1..10..3}
1 4 7 10
$ echo {01..05}
01 02 03 04 05

A prefix or suffix outside the braces is attached to every item, and two brace groups multiply:

$ echo file{1,2}.txt
file1.txt file2.txt
$ echo {a,b}{1,2}
a1 a2 b1 b2

The practical uses all follow from that. Build a directory tree in one command, because mkdir receives three separate arguments:

$ mkdir -p proj/{src,docs,tests}; ls proj
docs
src
tests

An empty item makes a handy backup idiom, since config.yml{,.bak} expands to config.yml config.yml.bak:

$ cp config.yml{,.bak}; ls
config.yml
config.yml.bak
proj

What braces will not do

Quote the braces and the magic stops. A single item, such as {a}, is not a list, so it is left alone:

$ echo "{a,b,c}" '{a,b,c}' {a}
{a,b,c} {a,b,c} {a}

More important, braces run first, before variables are looked up. So the bound of a range can’t be a variable:

$ n=3; echo {1..$n}
{1..3}

Bash saw the text $n inside the range, decided it was not a number, left the braces alone, and only then expanded the variable. When the count lives in a variable, use seq 1 "$n" or a C-style loop, which the loops lesson covers. The order-of-expansion lesson lists the whole sequence.

One version note. On macOS’s /bin/bash 3.2.57, {01..05} printed 1 2 3 4 5 with no zero padding and {1..10..3} printed the literal text, because both arrived in Bash 4. {a..e} worked on 3.2. Bash 5.3 handled all of them.

Tilde expansion

The other expansion in this lesson is for home directories. An unquoted ~ at the start of a word becomes your $HOME. ~name becomes that user’s home, and ~+ and ~- are the current and previous directories. Output below has my scratch paths shortened to /home/reader and /work.

$ echo ~
/home/reader
$ echo ~+ ~-
/work/l24 /work
$ echo ~root
/var/root
$ echo ~nobodyhere
~nobodyhere

/var/root is where macOS keeps root’s home (it is /root on most Linux systems). An unknown user leaves the tilde literal rather than failing.

Quotes switch it off, which is the usual bug:

$ echo "~" '~'
~ ~

So cd "~/projects" fails with “no such directory”, and the fix is cd ~/projects or cd "$HOME/projects". Tilde also expands after an = in a word that looks like an assignment, which is why a variable can hold it:

$ v=~/bin; echo "$v"
/home/reader/bin
$ echo --prefix=~/opt
--prefix=~/opt
$ echo a=~/opt
a=/home/reader/opt

a=~/opt expanded and --prefix=~/opt did not, because only the first looks like a variable assignment. Don’t count on tilde expansion after an = in command arguments.

Preview before you run

Because braces are plain text generation, you can see exactly what a command will receive by putting echo in front of it. That is worth doing before anything destructive:

$ echo rm app.log{,.1,.2}
rm app.log app.log.1 app.log.2
$ rm app.log{,.1,.2}; ls | wc -l
       0

The preview shows three arguments, and the real command removed all three files. (The padding in the wc -l count is how BSD wc on macOS prints; GNU wc prints a bare 0.) The same trick works for globs, where echo shows what the wildcard matched.

About the video

Script Bit’s “Bash Tutorial Ep. 2 - Shell Expansions, Arithmetic” is 18 minutes 35 seconds and was published in April 2026. I could not read a transcript, but the description has chapters, and “brace expansion” runs from 0:24 and “tilde ~ expansion” from 3:30 to 4:38. The embed plays exactly that stretch (24 to 278 seconds), so one short clip covers both halves of this lesson. I have not checked which brace forms it shows, so the zero-padding, step and {1..$n} points above come from my own runs. It is in English. This replaced Nick Janetakis’s 12-minute brace-only video, which goes deeper on braces with mkdir, touch and curl examples and is now linked under deeper beside You Suck at Programming’s tilde video (judged by title). The same Script Bit video also supplies the clips for parameter expansion and command and arithmetic substitution.

Where this fits

Braces and tildes generate or rewrite words. The next lesson is about the expansion that runs a command and uses its output: command and arithmetic substitution.

Check yourself

  1. 01How is `echo {a,b}{1,2}` different from `echo [ab][12]`?

    The braces generate `a1 a2 b1 b2` whether or not any such files exist. The bracket glob matches only existing file names, and if none match it is left as written.

  2. 02You write `for i in {1..$count}` and get one iteration of the literal text `{1..3}`. Why?

    Brace expansion runs before variable expansion, so Bash sees `$count` as text, not a number. Use `for ((i=1; i<=count; i++))` or `seq 1 "$count"`.

  3. 03What does `mkdir -p proj/{src,docs,tests}` do?

    Bash expands it to three arguments, `proj/src proj/docs proj/tests`, before `mkdir` runs, so all three directories are created in one command.

  4. 04Why does `echo --prefix=~/opt` print a literal tilde, while `echo a=~/opt` does not?

    Tilde expansion applies at the start of a word or right after an `=` or `:` in a variable-assignment-looking word. `--prefix=` does not look like an assignment, so the tilde stays literal.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark