在前面的 Bash命令行处理流程详解中,
第九步的 wrod spliting 用到了 LFS
http://blog.chinaunix.net/u2/63316/showart_2152062.html
摘自man bash 的关于 IFS 的东西
IFS The Internal Field Separator that is used for word splitting after expansion and to split lines into words with the read builtin command. The default value is ``
1. 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
Explicit null arguments ("" or '') are retained. Unquoted implicit null arguments, resulting from the expansion of parameters that have no values, are removed. If a parameter with no value is expanded within double quotes, a null argument results and is retained.
bash splits only the results of expansions on IFS, using
POSIX.2 field splitting rules; sh splits all words on IFS.
2. $* Expands to the positional parameters, starting from one. When
the expansion occurs within double quotes, it expands to a sin-
gle word with the value of each parameter separated by the first
character of the IFS special variable. That is, "$*" is equiva-
lent to "$1c$2c...", where c is the first character of the value
of the IFS variable. If IFS is unset, the parameters are sepa-
rated by spaces. If IFS is null, the parameters are joined
without intervening separators.
3 . Any element of an array may be referenced using ${name[subscript]}. The braces are required to avoid conflicts with pathname expansion. If subscript is @ or *, the word expands to all members of name. These subscripts differ only when the word appears within double quotes. If the word is double-quoted, ${name[*]} expands to a single word with the value of each array member separated by the first character of the IFS special variable, and
${name[@]} expands each element of name to a separate word. When there are no array members, ${name[@]} expands to nothing. If the double-quoted expansion occurs within a word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the last parameter is joined with the last part of the original word. This is analogous to the expansion of the special parameters * and @ (see Special Parameters above). ${#name[subscript]} expands to the length of ${name[subscript]}. If subscript is * or @, the expansion is the number of elements in the array. Referencing an array variable without a subscript is equivalent to referencing element zero.
以下是Advanced Bash-Scripting Guide中关于IFS的论述
$IFS defaults to (space, tab, and newline), but may be changed, for example, to parse a comma-separated data file. Note that uses the first character held in $IFS. See .
bash$ echo "$IFS" |
![]() | $IFS does not handle whitespace the same as it does other characters. Example 9-1. $IFS and whitespace
|
(Many thanks, Stéphane Chazelas, for clarification and above examples.)