$_ CLI In Depth
Learning progress 0%
lesson expansion quoting word-splitting

The Order of Shell Expansions

6 min read Module 8: Expansion, Quoting & Word Splitting Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:11

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.

BASH3 lines
x=5
echo {1..$x}
echo {1..5}
TEXT2 lines
{1..5}
1 2 3 4 5

If 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.

BASH3 lines
echo ~
echo "~"
echo ~/notes
TEXT3 lines
/workspace
~
/workspace/notes

Tilde 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.

BASH3 lines
dir='my docs'
printf '[%s]\n' $dir
printf '[%s]\n' "$dir"
TEXT3 lines
[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.

BASH3 lines
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.

flowchart TB line["Input line"] --> brace["Brace expansion"] brace --> tilde["Tilde expansion"] tilde --> param["Parameter, arithmetic, command substitution"] param --> split["Word splitting on IFS"] split --> glob["Filename expansion"] glob --> quote["Quote removal"] quote --> run["Command runs with argument list"]

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.

BASH3 lines
q='"x"'
echo $q
echo "$q"
TEXT2 lines
"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.

The order of shell expansions

Visualizer

Bash rewrites each word in a fixed sequence: braces, tilde, parameters and substitutions, word splitting, pathnames, then quote removal.

1 / 6
the line as typed
echoword 0
~/"$name"word 1
$(date +%Y)word 2
*.txtword 3

Bash parses the line into four words. Nothing is expanded yet, so each word is still its literal text.

name="my docs"; printf "[%s]\n" ~/"$name" $(date +%Y) *.txt
Terminal lab · 3 steps · real Linux in your browser

Watch the expansion passes

Predict and record what brace, parameter, word splitting, and filename expansion do to the same words.

01
Brace expansion runs before parameter expansion
Brace expansion is textual, and it happens before $x is replaced. At that point {1..$x} is not a numeric range, so the braces stay. Run this line in /workspace. It sets the variable, then writes the result of the echo into a file. ``bash x=5; echo {1..$x} > brace-first.txt `` Run cat brace-first.txt. You should see the literal text with braces, not a list of numbers.
Show hint for step 1
Put x=5 and the echo on one line separated by a semicolon, and redirect the output to brace-first.txt.
02
Filename matching follows parameter expansion
A variable that holds a pattern is expanded first, then the unquoted result is matched against file names. Three files have been created in /workspace for this step. The pattern a-*.txt should match two of them. ``bash pattern='a-*.txt' echo $pattern > globs.txt `` Run cat globs.txt. It should list the two matching names on one line.
Show hint for step 2
Store the pattern in a variable, then echo the variable without quotes. Redirect the output to globs.txt.
03
Quoting keeps the pattern literal
Quoting the same variable stops filename expansion. The characters a-*.txt are written exactly as they are. Run this in /workspace. ``bash pattern='a-*.txt' echo "$pattern" > quoted.txt `` Run cat quoted.txt to confirm that the file contains the pattern, not the file names.
Show hint for step 3
Put double quotes around the variable in the echo, and redirect to quoted.txt.
Check yourself Question 1 of 4
Concept Check First expansion step recall

Which expansion does bash perform first on a word such as a{1,2}?

Recap & Key Takeaways

Bash applies expansions in a fixed order: brace, tilde, parameter and arithmetic with command substitution, word splitting, filename expansion, then quote removal.
Brace expansion is textual and happens before any variable is read, so {1..$x} does not form a range.
Word splitting and globbing apply only to unquoted results, including values stored in variables.
Quote removal only strips quote characters that were typed in the original line, not those that came from a value.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue