Word splitting and the order of expansion
The fixed sequence Bash follows before running a command, why unquoted variables split and glob, and how IFS, set -x and quote removal fit in.
Globbing and wildcardsQuoting - single, double and escapesParameter expansionBrace and tilde expansionCommand and arithmetic substitution
Word splitting and the order of expansion
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Bash expands in a fixed order: braces, tilde, then parameter, arithmetic and command substitution, then word splitting, then pathname expansion (globbing), then quote removal.
- Word splitting and globbing happen to the results of unquoted expansions. That is why `$var` can turn one value into several arguments, or a `*` stored in a variable into a file list.
- `IFS` decides where splitting happens. The default is space, tab and newline, and setting it for one `read` is safe, while changing it globally is a trap.
- Double quotes around an expansion suppress both splitting and globbing. `"$@"` keeps each argument whole, whereas `$@` and `$*` re-split them.
- `set -x` prints every command after expansion, so it shows exactly which words a command received.
One command line, several passes
By now you have met six kinds of expansion: braces, tilde, variables, command substitution, arithmetic, and globs. Each one rewrites part of your command line. The thing nobody tells you at first is that they run in a fixed order, one pass after another, and many “mysterious” shell bugs are just that order showing through.
Bash’s order for a simple command is:
- Brace expansion
- Tilde expansion
- Parameter (variable), arithmetic and command substitution, left to right
- Word splitting, applied only to the results of step 3 that were not in quotes
- Pathname expansion (globbing), also on unquoted results
- Quote removal, which strips the quote characters you typed
Think of an assembly line. Each station handles one kind of rewrite and passes the line on. The brace station can’t see through a variable, because variables haven’t been expanded yet. That is the reason {1..$n} didn’t work in the brace lesson. And the splitting station only sees what the substitution station produced.
Splitting is the surprising pass
Word splitting takes the unquoted result of an expansion and cuts it into words wherever it finds a character from IFS, the Internal Field Separator. By default that is space, tab and newline.
$ names="my notes.txt"
$ printf '<%s>\n' $names
<my>
<notes.txt>
$ printf '<%s>\n' "$names"
<my notes.txt>
Quoting the expansion stops the split. There is no splitting of text you typed literally, only of the results of expansions, which is why printf '<%s>\n' my notes.txt was never the problem.
An empty unquoted expansion vanishes completely, because splitting an empty string gives zero words. This is behind a famous error message:
$ empty=; [ $empty = x ] && echo yes
bash: [: =: unary operator expected
$ [ "$empty" = x ] || echo "no"
no
$ [[ $empty = x ]] || echo "no, and no error"
no, and no error
Bash handed [ the words =, x and ], with nothing on the left. The [[ ]] keyword is parsed by the shell itself, so it doesn’t split.
Globbing runs on the leftovers
After splitting, any word containing *, ? or [ is treated as a pattern. That includes words that came from a variable.
$ pat='*.csv'; echo $pat; echo "$pat"
a.csv b.csv
*.csv
The variable held a star, not a file list, but the unquoted expansion was globbed. When nothing matches, Bash leaves the pattern as written:
$ rm -f a.csv b.csv; echo *.csv; echo "*.csv"
*.csv
*.csv
Controlling the splitter with IFS
IFS is an ordinary variable, so you can change it. Splitting on commas:
$ IFS=, ; line='ada,infra,london'; printf '<%s>\n' $line; unset IFS
<ada>
<infra>
<london>
Changing IFS for the rest of a script affects every later unquoted expansion, which is hard to debug. The safer pattern scopes it to a single command, as in IFS=, read -r who team city <<< "$line", which printed ada | infra | london without touching the shell’s own setting. The reading input lesson uses this a lot.
"$@" versus the rest
Positional parameters show all of this at once. With two arguments, one two and three:
$ set -- "one two" three; printf '<%s>\n' $*; printf '<%s>\n' "$*"; printf '<%s>\n' $@; printf '<%s>\n' "$@"
<one>
<two>
<three>
<one two three>
<one>
<two>
<three>
<one two>
<three>
Only "$@" preserves the original arguments. Use it whenever you pass arguments along to another command.
Watching the order with set -x
set -x makes Bash print each command after expansion, with a + prefix. I put this in a script and ran it with Bash 5.3, with a file called my notes.txt present:
+ echo 1 2 3 4 hi /home/reader
1 2 3 4 hi /home/reader
+ w='a b'
+ echo a b 'a b' 'my notes.txt'
a b a b my notes.txt
The braces, arithmetic, command substitution and tilde were all resolved before echo started. In the second command the unquoted $w printed as bare a b, two words, while the quoted one printed as 'a b', one word. The trace quotes anything that contains a space, so it shows you the word boundaries. ShellCheck flagged the unquoted forms with SC2086 and SC2046. The debugging lesson goes further with both tools.
About the video
Kandi Brian’s “Shell Expansion Order Explained: What Bash Does to Your Command Before It Runs” is 22 minutes 17 seconds and was published in October 2026. It has no chapters and I could not read a transcript, but the description lists what it covers: the expansion order stage by stage (brace, then the left-to-right pass of tilde, parameter, arithmetic and command substitution, then word splitting, filename expansion and quote removal), why {1..$n} prints itself, how IFS decides the number of arguments, no-match and dotfile behaviour in globbing, and a quoting matrix of unquoted, double-quoted and single-quoted forms. That is this lesson’s whole scope, which the earlier pick did not reach. The whole video is embedded. It also goes on to GLOBSORT (new in Bash 5.3), a GRUB example and argument injection through filenames, which are beyond this lesson. It is in English per the title and description. The channel is small and new, so this is a pick on scope, not on track record, and I have not checked its demos. Mindful Technology’s “Double quotes vs NO quotes” (35 minutes, chapters for splitting, IFS and globbing) was the previous pick and is linked under deeper, with a short You Suck at Programming video on IFS, judged by title.
Where this fits
This ends the quoting and expansion group. You can now predict what words a command receives. Next, those commands go into a file you can run again: your first script.
Check yourself
Why does `[ $empty = x ]` print `unary operator expected` when `empty` is empty?
The unquoted empty value vanishes during word splitting, leaving `[ = x ]` with a missing left side. Quote it as `[ "$empty" = x ]`, or use `[[ ]]`, which doesn't split.
A variable holds `*.csv`. What does `echo $pat` print compared with `echo "$pat"`?
The unquoted form splits and then globs, so it lists matching files (or the literal pattern if nothing matches). The quoted form prints `*.csv` literally.
Which expansion runs first, brace expansion or variable expansion, and what does that imply for `{1..$n}`?
Brace expansion runs first. When it looks at `{1..$n}`, `$n` is still text, so it is not a valid range and the braces are left alone.
What is the difference between `"$@"` and `$@` for the arguments `"one two" three`?
`"$@"` gives two words, `one two` and `three`. Unquoted `$@` gives three words, because each argument is split again on whitespace.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.