Splitting a String Into Words
In this page:
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
#!/bin/bash
sentence="the quick brown fox"
for word in $sentence; do
echo "Word: $word"
done
Login to try C/C++/Java/PHP code in the editor
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
#!/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]}"
Login to try C/C++/Java/PHP code in the editor
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
#!/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"
Login to try C/C++/Java/PHP code in the editor
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
#!/bin/bash
line="red:green:blue"
IFS=':' read -a colors <<< "$line"
echo "Colors: ${colors[@]}"
echo "Count: ${#colors[@]}"
Login to try C/C++/Java/PHP code in the editor
- Assuming word splitting happens on quoted variables too; it only applies to unquoted expansions,
"$var"is never word-split regardless ofIFS. - Forgetting to restore
IFSto its original value after temporarily changing it; leaving a customIFSin place can silently break every other unquoted expansion later in the script. - Assuming
read -aor aforloop splits on the exact delimiter string you set inIFS; it splits on *any* single character contained inIFS, not the whole string as one multi-character delimiter.
IFS(Internal Field Separator) defines which characters are treated as word boundaries during unquoted expansion and byread.- The default
IFSis 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.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: