<分区>
出于好奇,我想知道 bash(或 head?)中的哪个变化导致了以下行为变化,
在 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-changelog,但没有什么明显的东西会导致这个变化(尽管我有一些非常模糊的感觉,我以前偶然发现过这个)。
提前致谢!