🎓 A1Learner
🔍
0 of 50 lessons completed

Shell Expansion

Shell expansion is the way the shell changes words before a command runs. The shell expands braces, tildes and globs first, and the command then sees the finished words.

Make words with braces

Braces make one word for each choice.

echo file-{a,b,c}.txt

Count with a brace range

A brace range makes numbers in order.

echo {1..3}

Your home folder with ~

The tilde stands for your home folder.

touch ~/note.txt
ls ~

Match files with *

The star matches any run of characters.

echo *.txt

Match one character with ?

The question mark matches exactly one character.

echo photos/beach.jp?

The same glob in zsh

Zsh expands globs the same way as bash.

echo photos/*.jpg

Example

mkdir -p backup/{2024,2025,2026}
ls -d backup/*

Key points

  • Brace expansion: {a,b} makes one word from each choice inside the braces.
  • Brace range: {1..3} makes the numbers 1, 2 and 3 in order.
  • Tilde expansion: ~ stands for your home folder.
  • Glob expansion: * matches any run of characters in a file name.
  • Question glob: ? matches exactly one character in a file name.

In this example, brace expansion makes three folder names from one word. Then mkdir -p makes all three folders. Then ls -d lists the folders themselves.

Note

Expansion happens before the command runs, so the command never sees the braces or the star.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

What does echo {a,b}.txt print?