bash 4.3.43 和 4.4.19 之间的变量引用差异

Variable quoting difference between bash 4.3.43 and 4.4.19

出于好奇,我想知道 bash(或头部?)中的哪个变化导致了以下行为变化,

在 4.4.19 中我们有以下行为,

# Assign "foo <new line> bar" to variabe 'var'
$ > var="foo
bar"

# Echo with and without quotes,
$ > echo "${var}"
foo
bar

$ > echo ${var} 
foo bar

# Read all lines (1) except the last, 
$ > head -1 <<< ${var}
foo 

$ > head -1 <<< "${var}"
foo

在 bash 4.2.46 和 4.3.43 中执行完全相同的操作会导致在使用 head 读取变量时产生不同的输出。

# Assign "foo <new line> bar" to variabe 'var'
$ > var="foo
bar"

# Echo with and without quotes,
$ > echo "${var}"
foo
bar

$ > echo ${var} 
foo bar

$ > head -1 <<< ${var}
foo bar

$ > head -1 <<< "${var}"
foo

所以在我看来(4.4.19)无论你是否引用变量,head 的输入都是两行。对于版本 4.2.46 和 4.3.43,输入实际上有所不同,具体取决于您是否引用变量。

前面的行为对我来说很有意义,如果你想要换行,你必须引用变量。我真的对这种行为改变及其背后的原因很感兴趣。我尝试查看 bash-更新日志,但没有什么明显的东西会导致此更改(尽管我有一些非常模糊的感觉,我以前偶然发现过这个)。

提前致谢!

bash 4.3 中的行为是一个错误,已在 4.4 中修复。请参阅 original bug reportChet's reply (2015/09).

bash 邮件列表中与此相关的最新帖子:the question and Chet's reply (2017/11)。