12. Word Splitting

After all the expansion from above are done, word splitting takes place: The remaining unquoted characters are splitted into words using each character within the variable IFS as possible separator. "space", "tab" and "newline" are used as defaults:

$ unset IFS
$ words=($(echo -e "Oranges and lemons\nsay the bells of\tSt. Clement's"))
echo ${#words[@]}

The -e option treats the echo command to convert the escape sequences to special chars. After echoing the string includes eight separators from the IFS parameter: six blanks, one newline (\n) and one tab (\t). So the characters are splitted into nine words. Hence, the array words is initialized by nine words. Putting double quotes around the echo command treats Bash to suppress the word splitting:

$ words=("$(echo -e Oranges and lemons\nsay the bells of\tSt. Clement\'s)")
echo ${#words[@]}

Finally the array words is initialized by one word instead of nine. It's common to surround variables with double quotes. This prevents the variables value to be splitted into several words:

$ file="my file"
$  ls "$file" $file
ls: cannot access file: No such file or directory
ls: cannot access my: No such file or directory
my file

Words are not only performed by word splitting. They are performed by: brace expansion, filename expansion word splitting and the use of the subscripts "@" or "*":

$ words=(bin/* ${fruits[@]} A Clockwork Orange {a..c})
$ echo ${#words[@]}

The first three words orginates from filename expansion "bin/*":

$ ls ${words[@]:0:3}
bin/logrotate.sh bin/lstree.sh bin/pstree.sh

the next five from parameter expansion "${fruits[@]}":

$ echo ${words[@]:3:5}
orange lemon apple melone banana

the next three from word splitting:

$ echo  ${words[@]:8:3} 
A Clockwork Orange

and the last three from brace expansion "{a..c}":

$ echo ${words[@]:11:3}
a b c