← Back to Bash Course | Chapter 7: String Manipulation | Lesson 8 of 10

Splitting a String Into Words

Bash automatically breaks unquoted text apart into separate words wherever it finds characters listed in the special IFS variable, which is normally just space, tab, and newline.

The Default IFS Behavior

By default, IFS contains a space, a tab, and a newline, and Bash uses these characters to split unquoted variable expansions into separate words. This is what lets a plain for word in $sentence loop naturally iterate over each space-separated word.

Example: The Default IFS Behavior

bash
#!/bin/bash
sentence="the quick brown fox"
for word in $sentence; do
    echo "Word: $word"
done

Splitting on a Custom Delimiter

Temporarily setting IFS to a different character, like a comma, changes what Bash treats as a word boundary for unquoted expansions and for read. This is a common technique for parsing simple delimited data like CSV-style lines.

Warning: Always restore the original IFS afterward, as shown, so later parts of the script aren't affected by the custom delimiter.

Example: Splitting on a Custom Delimiter

bash
#!/bin/bash
csv_line="apple,banana,cherry"
old_ifs="$IFS"
IFS=","
read -a fields <<< "$csv_line"
IFS="$old_ifs"
echo "Field count: ${#fields[@]}"
echo "Second field: ${fields[1]}"

Quoting Prevents Word Splitting

Wrapping a variable expansion in double quotes disables word splitting entirely, keeping the whole value as one unit no matter what IFS is set to. This is why quoting variables is the default safe habit, and unquoted expansion is reserved for situations where you specifically want splitting.

Example: Quoting Prevents Word Splitting

bash
#!/bin/bash
data="one two three"
echo "Quoted (one word): \"$data\""
count=0
for part in $data; do
    count=$((count + 1))
done
echo "Unquoted split into $count words"

Splitting Into an Array with a Custom IFS

Combining a custom IFS with read -a is a clean way to split a single delimited line directly into an array in one step. Scoping the IFS change to just that one command (by setting it inline before read) avoids having to manually save and restore it.

Warning: Setting IFS='...' command on the same line as the command scopes the change to just that one command, leaving the shell's own IFS untouched afterward.

Example: Splitting Into an Array with a Custom IFS

bash
#!/bin/bash
line="red:green:blue"
IFS=':' read -a colors <<< "$line"
echo "Colors: ${colors[@]}"
echo "Count: ${#colors[@]}"
Common Mistakes
  1. Assuming word splitting happens on quoted variables too; it only applies to unquoted expansions, "$var" is never word-split regardless of IFS.
  2. Forgetting to restore IFS to its original value after temporarily changing it; leaving a custom IFS in place can silently break every other unquoted expansion later in the script.
  3. Assuming read -a or a for loop splits on the exact delimiter string you set in IFS; it splits on *any* single character contained in IFS, not the whole string as one multi-character delimiter.
Chapter Summary
  • IFS (Internal Field Separator) defines which characters are treated as word boundaries during unquoted expansion and by read.
  • The default IFS is space, tab, and newline; changing it lets you split on other characters like commas.
  • Word splitting only happens on unquoted expansions -- quoting a variable with "$var" disables it entirely.
  • Always save and restore the original IFS (or scope the change to a subshell/local context) after temporarily customizing it, to avoid affecting the rest of the script.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.