What are tilde expansion and brace expansion in bash?
Tilde expansion turns ~ into a home directory path; brace expansion turns {a,b,c} or {1..5} into multiple strings — both are text the shell generates before the command runs.
These differ from globbing in a key way: globbing matches files that already exist, but brace expansion is pure text generation — it produces strings whether or not any matching file is there. That's why brace expansion is perfect for creating things.
Tilde (~) is a shorthand for home directories:
| You type | Shell substitutes |
|---|---|
~ |
your own home directory |
~/Documents |
your home + /Documents |
~alice |
the user alice's home |
Braces ({}) expand a list or a range into several arguments:
mkdir -p project/{src,bin,docs} # makes three dirs in one command
touch file{1..10}.txt # creates file1.txt ... file10.txt
cp file.txt{,.bak} # clever: expands to "cp file.txt file.txt.bak"
echo {Mon,Tues,Wednes,Thurs,Fri}day
That cp file.txt{,.bak} trick is worth understanding: {,.bak} expands to two items — an empty string and .bak — giving cp file.txt file.txt.bak, the idiomatic one-liner for "make a backup copy."
Order tip: brace expansion happens before glob expansion. So ls {a,b}*.txt first becomes ls a*.txt b*.txt, and then each * is matched against real files.