Bash Word splitting What, when and Why?


When the shell performs parameter expansion, command substitution, variable or arithmetic expansion, it scans for word boundaries in the result. If any word boundary is found, then the result is split into multiple words at that position. The word boundary is defined by a shell variable IFS (Internal Field Separator). The default value for IFS are space, tab and newline, i.e. word splitting will occur on these three white space characters if not prevented explicitly.

set -x
var='I am
multiline string'
fun() {
    echo "-$1-"
    echo "*$2*"
    echo ".$3."
fun $var

In the above example this is how the fun function is being executed:

fun I am a multiline string

$var is split into 5 args, only I, am and a will be printed.