Word Splitting
The shell scans the results of parameter expansion, command substitution, and arithmetic expansion that did not occur within double quotes for word splitting.
The shell treats each character of IFS as a delimiter, and splits the results of the other expansions into words on these characters. If IFS is unset, or its value is exactly <space><tab><newline>, the default, then sequences of <space>, <tab>, and <newline> at the beginning and end of the results of the previous expansions are ignored, and any sequence of IFS characters not at the beginning or end serves to delimit words. If IFS has a value other than the default...
词分割
shell扫描不在双引号中发生的parameter expansion, command substitution, and arithmetic expansion操作的结果执行词分割。
shell把IFS中的每一个字符当做定界符,当见到这些字符时就把展开的结果分割为词。如果IFS没设置或者它的值恰好是默认值<space><tab><newline>,那么前一个展开的结果中在开始或结束位置的<space><tab><newline>序列就被丢弃了,不在开始和结束位置的IFS字符序列用来界定(分割)词。
应用举例
比如我要输出IFS的具体字符,在shell脚本中就必须阻止 词分割。
#!/bin/bash
printf "%-4c%-4d\n" ${IFS:0:1} \'${IFS:0:1}
printf "%-4c%-4d\n" ${IFS:1:1} \'${IFS:1:1}
printf "%-4c%-4d\n" ${IFS:2:1} \'${IFS:2:1}
printf "%-4c%-4d\n" "${IFS:0:1}" \'"${IFS:0:1}"
printf "%-4c%-4d\n" "${IFS:1:1}" \'"${IFS:1:1}"
printf "%-4c%-4d\n" "${IFS:2:1}" \'"${IFS:2:1}"
输出:
[root@iZm5eexacs7wlytnyle3n8Z script]# wordsplit.sh
' 0
' 0
' 0
32
9
10
可见,如果 printf的参数不加双引号的话,输出的值是空,因为IFS中的每一个字符都是 定界符,参数展开的值都被丢弃了,它们都变成了空值。