Linux Command Reference ยท Concepts
Order of Bash expansions
Reference for the exact order Bash expands a command line: brace, tilde, parameter, arithmetic, command substitution, word splitting, globbing and quote removal.
Before Bash runs a command it transforms the words you typed in a fixed sequence of expansions. Each step sees the output of the previous one and never goes back, which explains most "why did my variable not work there?" puzzles. This page is the compact reference; the lesson The Order of Expansions in Depth walks through each stage slowly.
brace -> tilde -> parameter / arithmetic / command substitution -> word splitting -> pathname (glob) -> quote removal
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.
The stages in order
- History expansion (
!!): interactive shells only, before the line is parsed. Off in scripts. - Tokenizing: the line is split into words and operators. Quoting decided here prevents later changes.
- Brace expansion:
{a,b}and{1..5}produce several words. Purely textual: nothing is looked up, and variables are not yet expanded. - Tilde expansion: a leading unquoted
~or~user; also after:or=in assignments. - Parameter and variable expansion, arithmetic expansion, command substitution, and process substitution: performed together, left to right.
- Word splitting: only the unquoted results of stage 5 are split at
IFScharacters (space, tab, newline by default). - Pathname expansion (globbing): words with unquoted
*,?or[...]are replaced by sorted matching paths. - Quote removal: the quote characters and backslashes that did their job are deleted.
Redirections are performed left to right, and their targets are expanded with the same rules. A target that expands to more than one word (an unquoted variable with a space in it, say) is an "ambiguous redirect" error.
Three rules that fall out of this
- A stage never sees the future.
{1..$n}fails because braces come first.~stored in a variable is not turned into your home directory because tilde expansion came first. - Results are not re-scanned. If
cmd='echo $HOME'then$cmdrunsechowith the single argument$HOME, not your home path. Text in variables is data.evalis the deliberate way to scan again, and it is whyevalis dangerous with untrusted input. - Quoting disables stages selectively.
| Quoting | Braces, tilde | Variables, $( ), $(( )) |
Word splitting | Globbing |
|---|---|---|---|---|
| none | yes | yes | yes (on results) | yes |
"double" |
no | yes | no | no |
'single' |
no | no | no | no |
\ before one char |
that char literal | that char literal | no | no |
("$@" and "${arr[@]}" are special: inside double quotes they still produce one word per element.)
Usage notes
- Unquoted
$varmeans split and glob. A value with spaces becomes several arguments, and a value with*turns into filenames. Quote variables unless you want exactly that. - Prefix assignments (
VAR=x cmd) are applied after the command's arguments are expanded, soVAR=x echo $VARprints the old value. - Arrays and
"${a[@]}"are expanded in stage 5 and keep element boundaries, which is the reliable way to build argument lists. - Different order from other shells: zsh does not word-split unquoted variables by default, and POSIX
shhas no brace expansion at all. See POSIX sh vs Bash. - Debug with
set -xto see the line after expansion (+ echo a b), or withprintf '[%s]\n' ...to see word boundaries. - Sequence of side effects:
$((i++))and${v:=default}change state while the line is being expanded, so two expansions on one line can see different values. IFSchanges affect only step 6 of the same line if set earlier;IFS=, cmdas a prefix does not affect the expansions of that very line.