Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Bash Syntax

Bash word splitting

Reference for Bash word splitting: how unquoted expansions are split by IFS, why quoting prevents bugs, and how to use IFS with read and arrays.

After Bash expands a variable, a command substitution or an arithmetic expression, it checks whether the result was written without quotes. If so, it splits the result into separate words at every character found in IFS, the internal field separator. That is word splitting.

It is the reason rm $file fails when the name contains a space, and the reason the quoting advice "always quote your variables" exists. Splitting only affects the results of expansions, never the literal text you typed.

Terminal
list="a b c"
for x in $list; do echo "$x"; done     # three iterations
for x in "$list"; do echo "$x"; done   # one iteration

Try it

Bash Splitting and the cure

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Using IFS on purpose

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
IFS=" $'\t\n'"     # default value: space, tab, newline
IFS=:              # split on colons
IFS=               # empty: no splitting at all
Rule Description
What is split The results of unquoted parameter expansion, command substitution and arithmetic expansion.
Separator characters Every character in IFS. Whitespace characters in IFS (space, tab, newline) merge into one separator; other characters each separate fields and can create empty fields.
Unset IFS Behaves like the default value.
Empty IFS No splitting is done.
Quoted expansions Not split. "$@" and "${array[@]}" are the special cases that produce one word per element.

Returns: zero or more words. An unquoted expansion of an empty value produces no word at all.

Usage notes

  • Quote every expansion that should stay one word: "$var", "$(cmd)", "$@". This is the cure for almost every word splitting bug.
  • Splitting then globbing: the words resulting from splitting are also subject to pathname expansion, so a value like * can expand to file names. Quoting prevents both.
  • IFS and read: IFS also controls how read splits a line into variables. Prefixing IFS=: to read changes it for that command only.
  • Do not leave IFS modified. Changing it globally breaks later code; use a subshell, a prefix assignment, or save and restore it.
  • $* joins with the first character of IFS: IFS=,; echo "$*" prints a,b,c.
  • Safer ways to build lists: use arrays (files=(...)), mapfile (mapfile) and find -print0 with read -d '' for arbitrary file names.
  • for f in $(ls) is a bug: it splits file names at spaces. Use for f in * instead.
  • Zsh differs: Zsh does not split unquoted expansions by default, so scripts that depend on it are not portable between shells.
  • Not applied in assignments (x=$y), inside [[ ]], or to the word after case.