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.
v='a b'
printf '[%s]\n' $v
printf '[%s]\n' "$v"
printf '[%s]\n' a b[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.
p='a::c'
( IFS=:; set -- $p; echo "$# [$2]" )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.
IFS=: read -r user shell rest <<< 'root:x:0:0:root:/root:/bin/bash'
echo "$user $shell"root xSplitting 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.
list='one two'
for w in $list; do echo "[$w]"; done
for w in "$list"; do echo "[$w]"; done[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.