Skip to content
Tutorials Center
My learning

Topics

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.

TEXT
brace -> tilde -> parameter / arithmetic / command substitution -> word splitting -> pathname (glob) -> quote removal

Try it

Bash Braces run before variables

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.

Bash Watching each stage with a helper

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

  1. History expansion (!!): interactive shells only, before the line is parsed. Off in scripts.
  2. Tokenizing: the line is split into words and operators. Quoting decided here prevents later changes.
  3. Brace expansion: {a,b} and {1..5} produce several words. Purely textual: nothing is looked up, and variables are not yet expanded.
  4. Tilde expansion: a leading unquoted ~ or ~user; also after : or = in assignments.
  5. Parameter and variable expansion, arithmetic expansion, command substitution, and process substitution: performed together, left to right.
  6. Word splitting: only the unquoted results of stage 5 are split at IFS characters (space, tab, newline by default).
  7. Pathname expansion (globbing): words with unquoted *, ? or [...] are replaced by sorted matching paths.
  8. 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 $cmd runs echo with the single argument $HOME, not your home path. Text in variables is data. eval is the deliberate way to scan again, and it is why eval is 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 $var means 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, so VAR=x echo $VAR prints 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 sh has no brace expansion at all. See POSIX sh vs Bash.
  • Debug with set -x to see the line after expansion (+ echo a b), or with printf '[%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.
  • IFS changes affect only step 6 of the same line if set earlier; IFS=, cmd as a prefix does not affect the expansions of that very line.