Multiple variable assignment using 'read' works in bash v4.3.48 and does not in v4.4.7

我们两清 提交于 2019-12-11 04:33:09

问题


I'm using two distros which use bash v4.3.48 and v4.4.7.

read VAR1 VAR2 <<< $(echo 0 ; echo 1)
echo $VAR2

For bash v4.3.48, the result of commands above is $VAR2 has value 1. But, with bash 4.4.7, $VAR2 is null.

To get the same result, in 4.4.7, I must modify the script:

read VAR1 VAR2 <<< $(echo -n $(echo 0 ; echo 1) )

I don't know my (previous) script was wrong or there're changes in the newer bash.


回答1:


It looks like the behavior when expanding <<< $( ) has changed slightly. Normally, when $( ) occurs without double-quotes around it, the result undergoes word splitting and wildcard expansion. But when it's after <<< it appears that early versions of bash skip the wildcard expansion part but do word-split it and then paste the result back together with spaces. You can see this by using cat instead of read:

$ echo $BASH_VERSION
4.2.10(1)-release
$ cat <<< $(echo 0; echo 1)
0 1
$ cat <<< $(echo '*       *'; echo 1)
* * 1

Note that the extra spaces in '* *' have vanished, and the line breaks between the two echoed string have turned into spaces, but the wildcards didn't get expanded into a list of files. As a result, when you use read VAR1 VAR2 <<< $(echo 0 ; echo 1), read receives "0 1" and puts those digits in the two variables.

On the other hand, newer versions of bash skip the word splitting as well:

$ echo $BASH_VERSION
4.4.12(1)-release
$ cat <<< $(echo 0; echo 1)
0
1
$ cat <<< $(echo '*       *'; echo 1)
*       *
1

That means that when you use read VAR1 VAR2 <<< $(echo 0 ; echo 1), the read command receives two lines: "0" and "1", but it only reads a single line ("0") and so only $VAR1 gets a value.

Update: Eric Renouf spotted the change in the release notes for bash-4.4-beta:

z. Bash no longer splits the expansion of here-strings, as the documentation has always said.



来源:https://stackoverflow.com/questions/45177871/multiple-variable-assignment-using-read-works-in-bash-v4-3-48-and-does-not-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!