bash: IFS "stuck" 暂时更改为阵列构建后

bash: IFS "stuck" after temporarily changing it for array building

为了建阵临时改IFS后遇到了一个奇怪的问题:

$ echo "1 2 3" |while read myVar1 myVar2; do echo "myVar1: ${myVar1}"; echo "myVar2: ${myVar2}"; done
myVar1: 1
myVar2: 2 3
$ IFS=':' myPaths=( ${PATH} )  # this works: I have /home/morgwai/bin on ${myPaths[0]} , /usr/local/sbin on ${myPaths[1]} and so on
$ echo "1 2 3" |while read myVar1 myVar2; do echo "myVar1: ${myVar1}"; echo "myVar2: ${myVar2}"; done
myVar1: 1 2 3
myVar2: 
$ echo $IFS

$ echo "1:2:3" |while read myVar1 myVar2; do echo "myVar1: ${myVar1}"; echo "myVar2: ${myVar2}"; done ;
myVar1: 1
myVar2: 2:3

通常,当我临时更改 IFS 以用于除数组构建之外的任何其他命令(例如 IFS=',' echo whatever)时,它的值仅在执行期间更改,但是这里似乎 IFS 被永久更改为冒号(虽然 echo $IFS 没有显示这个,这更奇怪......)。

这是错误还是某种我不理解的预期行为?
如果重要的话,我正在使用 bash 版本 4.4.18...

注意:我知道我可以使用 IFS=':' read -a myPaths <<< ${PATH} 构建相同的数组,然后 IFS 通常恢复为默认值,但这不是重点:我'我试图理解我在上面给出的示例中实际发生了什么。

谢谢!

你只是设置变量,而不是设置一个变量然后执行命令(即,你构建数组的方式是一个纯变量赋值,而不是一个命令,因此这两个赋值都是永久性的)。

:IFS 没有出现在 echo $IFS 中的问题是由 shell 参数扩展和分词引起的。

考虑:

$ IFS=:
$ echo $IFS

$ echo "$IFS"
:

当一个参数扩展没有被引用时,它经历分词后缀。来自 the manual:

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 using these characters as field terminators. 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.

因此,当 IFS 是冒号时,拆分仅由冒号组成的单词会产生(单个)空单词。总是引用你的变量来防止像这样的意外问题。