Quoting and expansion
12 minThe eight stages
What the shell does to a line before anything runs.
Before a command runs, the shell rewrites the line you typed. It does that in eight stages, always in the same order, and almost every surprising thing a shell does is a consequence of that order rather than of any single feature.
The order is: brace expansion, tilde expansion, parameter expansion, command substitution, arithmetic expansion, word splitting, pathname expansion, quote removal.
Try this
Press expand beside any command you run to see it move through all eight stages. That view is the point of this whole track.
Two consequences fall straight out of the order. Brace expansion happens first, before any variable has a value — so a brace stored in a variable is just text.
Pathname expansion happens nearly last, after parameter expansion — so a glob stored in a variable does expand, as long as it is used unquoted.
Try this
A pattern that matches nothing is not an error and does not disappear. The shell leaves it exactly as written, and the command then receives a literal *.md and complains about a file with that name.
That is why an empty directory produces a confusing message from the utility rather than a clear one from the shell.
Try this
Files
| Mode | Links | Size | Name |
|---|---|---|---|
| -rw-r--r-- | 1 | 62 | agenda.txt |
| -rw-r--r-- | 1 | 128 | changelog.txt |
| -rw-r--r-- | 1 | 104 | receipt.txt |
| -rw-r--r-- | 1 | 65 | summary.md |