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

Word Splitting & IFS

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

What gets split

Word splitting applies to the results of unquoted parameter expansion, command substitution, and arithmetic expansion. It does not apply to literal text that you typed, and it does not apply to anything inside double quotes. Spaces that you type without quotes are separators before any expansion happens, which is why printf '[%s]\n' a b passes two arguments. Word splitting is a separate step that applies only to the text that expansions produce. The rule matters because it tells you where to look for trouble: an unquoted variable or substitution is the usual suspect.

BASH4 lines
v='a  b'
printf '[%s]\n' $v
printf '[%s]\n' "$v"
printf '[%s]\n' a  b
TEXT5 lines
[a]
[b]
[a  b]
[a]
[b]

The default separators

When IFS is unset, bash uses three characters: space, tab, and newline. Any run of those characters separates two fields, and leading or trailing runs produce no empty fields. This is why set -- $v with a value that starts and ends with spaces gives you only the words in the middle. The sequence of whitespace is treated as one separator, so two spaces and six spaces both count as a single break.

The set -- builtin is a good way to watch splitting, because it replaces the positional parameters and then $# tells you how many words the expansion produced. The lab for this lesson uses it to count words in both the quoted and unquoted forms.

Non-whitespace separators make empty fields

A character in IFS that is not whitespace behaves differently. Each occurrence of such a character ends a field, and two of them in a row produce an empty field between them. Colon-separated values such as PATH rely on this rule, and so do many data formats. The example below sets IFS to a colon for a single command group, so the change does not leak into the rest of the shell.

BASH2 lines
p='a::c'
( IFS=:; set -- $p; echo "$# [$2]" )
TEXT1 lines
3 []

The three fields are a, an empty field, and c. The empty field is the one that often disappears by surprise. A mixed separator such as a colon and a space is also possible, and the whitespace characters in the set collapse while the other separators keep their empty fields.

Setting IFS for one command

You rarely need to change IFS for the whole shell. Prefix the assignment to a single command, as in IFS=: read -r a b c, and the change applies only to that command. The read builtin uses IFS to split the line it reads. Pair it with -r, which stops backslashes from being treated as escape characters. The last variable receives whatever is left of the line, so the field boundaries are preserved for the names that come first.

BASH2 lines
IFS=: read -r user shell rest <<< 'root:x:0:0:root:/root:/bin/bash'
echo "$user $shell"
TEXT1 lines
root x

Splitting in loops and arguments

A for loop is one of the most common places where splitting surprises people. The list after in is subject to word splitting, so a variable that holds several words produces several iterations. Quoting the variable inside the list keeps the value as one item, which is usually what you want for a path or a message. The same reasoning applies to the arguments of any command. An unquoted variable that holds a directory name with a space becomes two arguments, and the command may fail or act on the wrong file.

BASH3 lines
list='one two'
for w in $list; do echo "[$w]"; done
for w in "$list"; do echo "[$w]"; done
TEXT3 lines
[one]
[two]
[one two]

Splitting also breaks the test command in a subtle way. If the unquoted variable is empty, it disappears entirely, so [ $x = y ] becomes [ = y ] and the test receives the wrong number of arguments. If the variable holds a space, the test receives extra arguments. Quote the variable in every test, as in [ "$x" = y ], and the test always receives exactly three words.

Positional parameters and splitting

The positional parameters follow the same rules. The quoted form "$@" expands to one word per argument, even when an argument contains spaces, which is why it is the form to use when passing a script's arguments on to another command. The quoted "$*" joins all the arguments into one word, separated by the first character of IFS. An unquoted $@ or $* is split again, so the arguments that held spaces are broken apart. The rule of thumb is to write "$@" whenever you forward arguments.

Turning splitting off

Setting IFS to an empty string disables word splitting entirely. Unquoted expansions then produce one word each, which is convenient but unusual. The more common idiom is the empty assignment in front of read, as in IFS= read -r line. It preserves leading and trailing whitespace in the line, which the default separators would strip. Use it whenever you read a line that might contain significant spaces.

Splitting and globbing together

Splitting runs before filename expansion, so a pattern that appears in the value of an unquoted variable is split and then matched. A value such as *.log becomes a list of matching names, and a value with spaces becomes several patterns, each of which is matched separately. Quoting the expansion stops both steps at once, which is a good reason to quote by default. The next lessons show how the same reasoning applies to file names that contain spaces, newlines, or leading dashes.

A practical habit follows. Look at every unquoted expansion in a script and ask whether it could ever contain a space, a tab, or a newline. If the answer is yes, quote it. If the answer is no, the quotes are harmless, and they make the next reader more confident. Most people find that the quoting habit removes an entire category of failures, and the cost is only a pair of characters.

Word splitting and quoting a value with spaces

Visualizer

An unquoted variable is split into words at spaces, tabs and newlines. A filename with a space breaks unless the expansion is quoted.

1 / 5
value of f
m
y
␠
f
i
l
e
.
t
x
t
IFS
space, tab, newline (default)

One variable holds one string with a space in it. The shell only sees characters; it does not know the value is a filename.

f="my file.txt"; touch "$f"; ls $f; echo "status $?"; ls "$f"
Terminal lab · 3 steps · real Linux in your browser

Count fields and split on separators

Compare quoted and unquoted expansions, split on a non-whitespace separator, and use read with a custom IFS.

01
Compare unquoted and quoted counts
Store a value with extra spaces. The unquoted expansion splits it into three words, and the quoted expansion stays one word. Run these lines in /workspace. ``bash s=' alpha beta gamma ' set -- $s; echo $# > unquoted.txt set -- "$s"; echo $# > quoted.txt `` The first file should contain 3 and the second should contain 1.
Show hint for step 1
Set s with spaces around the words, then count with set -- once unquoted and once quoted. Redirect each count to a file.
02
Keep the empty field between colons
Set IFS inside parentheses so the change ends with the subshell. Then set the positional parameters from the value and print the count and the second field. ``bash p='a::c' ( IFS=:; set -- $p; echo "$# [$2]" > colon.txt ) `` The file should contain 3 followed by an empty pair of brackets.
Show hint for step 2
Run the split in a subshell with IFS set to a colon, so the change does not leak. Write the output to colon.txt.
03
Split a passwd-style line with read
The read builtin splits the here-string at each colon. The last name, rest, takes the remaining text. Write the first two names to a file. ``bash IFS=: read -r user shell rest <<< 'root:x:0:0:root:/root:/bin/bash' echo "$user $shell" > passwd.txt `` The file should contain root x.
Show hint for step 3
Prefix the read command with IFS=: and give it three names. Write the first two fields to passwd.txt.
Check yourself Question 1 of 4
Concept Check Default separators recall

With IFS unset, which characters does word splitting use to separate fields?

Recap & Key Takeaways

Word splitting applies only to unquoted parameter, command substitution, and arithmetic results, never to typed literal text or to quoted expansions.
By default IFS is space, tab, and newline, and runs of those characters act as one separator.
A non-whitespace character in IFS makes an empty field each time it appears twice in a row.
Prefix IFS to a single read command, and use IFS= read -r to keep leading and trailing spaces.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue