One line, many passes
When you press Enter, bash reads the line, splits it into words, and then transforms those words before it runs anything. The transformations are called expansions. The bash manual lists them in a specific order, and the order matters because the output of one step becomes the input of the next. The main steps, in the order bash performs them, are brace expansion, tilde expansion, parameter and variable expansion, arithmetic expansion, command substitution, word splitting, filename expansion (globbing), and finally quote removal.
Some of these steps happen in the same pass. Parameter expansion, arithmetic expansion, and command substitution are performed together, from left to right across the word. Process substitution, written as <(command), is also handled in that group. The important point is that the sequence has a shape: text is rewritten first, values are inserted second, the result is split into words third, and pattern matching against file names comes after that.
Brace expansion runs first
Brace expansion is purely textual. Bash rewrites a{x,y}b as axb ayb before it looks at any $ sign. That ordering surprises people who try to build a numeric range from a variable. Set x=5, then ask for {1..$x}. The brace step sees a range whose end point is not a number, so it leaves the word alone. Only later does the parameter step replace $x with 5, which leaves the literal text {1..5} on the screen.
x=5
echo {1..$x}
echo {1..5}{1..5}
1 2 3 4 5If you need a variable range, use a C-style loop or seq instead. The brace step is convenient for literal lists such as mkdir logs/{2024,2025}, where every alternative is known while you type.
Tilde expansion comes next
Tilde expansion is the step right after braces. A tilde at the start of a word, followed by nothing or by a user name, is replaced with a home directory. A bare ~ becomes the value of HOME, and ~/notes becomes that directory with /notes appended. The lookup is done during this step, so the expansion uses the value that HOME has when the line runs, not the value it had when the shell started. A tilde that is quoted, such as "~", is left alone, and a tilde in the middle of a word is not an expansion at all.
echo ~
echo "~"
echo ~/notes/workspace
~
/workspace/notesTilde expansion is also a good example of why the order matters. Because it runs before parameter expansion, a value such as '~/notes' stored in a variable is not turned into a home path when you read it back with echo $d. Bash does expand a tilde that appears in an assignment, such as d=~/notes, because it handles that case at assignment time. Keep the tilde at the start of a literal word and let the rest of the path come from quoted variables.
Parameters, then word splitting
Parameter expansion returns the value of a variable, but the value itself is not checked for spaces at that moment. Splitting happens later, and it applies only to unquoted results. Set dir to a value with a space and compare two versions of the same printf call.
dir='my docs'
printf '[%s]\n' $dir
printf '[%s]\n' "$dir"[my]
[docs]
[my docs]The unquoted $dir produced two words, because the value contained a space and the space is one of the default separators. The quoted version kept a single word. The same reasoning explains why a variable that holds a pattern can be used to select files without any special syntax.
Globbing sees the result of the earlier steps
Filename expansion is the last of the rewriting steps. It looks at unquoted words that contain *, ?, or [ and replaces them with the matching names in the current directory. Because parameter expansion came earlier, a pattern stored in a variable is matched exactly like a pattern typed on the line.
pattern='*.txt'
echo $pattern
echo "$pattern"The first echo prints the names of the text files in the current directory. The second prints the literal characters *.txt, because quoting stops filename expansion. If no file matches, bash leaves the unquoted pattern as it is, unless the nullglob option is set.
Expansion happens once per word
A subtle consequence of the order is that each expansion produces its result from the original word, not from the output of the expansion that ran just before it on the same word. Bash does not run a text through every step repeatedly until nothing changes. A command substitution that prints a brace pattern does not get brace-expanded afterward, and a variable whose value looks like {a,b} is not brace-expanded when you read it back. Those values do reach the later steps, which is why a variable holding a glob pattern is still matched against file names. Each step has one chance to act on the word that reaches it, and that is why the order can be learned as a short list.
Quote removal is the last step
After every expansion has finished, bash removes the quote characters that were typed in the original line. That is why echo "a"'b' prints ab. The rule has a subtle edge. Quote removal applies to quote characters that appeared in the input, not to quote characters that arrived inside a variable value. Consider this case.
q='"x"'
echo $q
echo "$q""x"
"x"Both lines print the quote marks, because they came from the value of q, not from the original text of the line. Bash only removed the quotes that the reader typed.
Reading a whole line
Put the steps together with one more example. Set n=2 and run echo item{1..2}-$n.log. Brace expansion first turns the word into two words, item1-$n.log and item2-$n.log. Parameter expansion then fills in the value of n in each word. Splitting and globbing find nothing special, and the command prints item1-2.log item2-2.log. Reading the line in this order is the reliable way to predict bash, and it is also how you debug a surprising result: run the same line with set -x, or with echo in front of it, and look at the words that bash actually passes to the command.
A practical habit follows from this order. If a word must stay exactly as typed, quote it. If a word must become several arguments, leave it unquoted and make sure the separators are the ones you intend. The rest of this module returns to each step in detail, starting with the variable forms and then the command substitution that produces values.