linux - bash 4.3.43 和 4.4.19 之间的变量引用区别

标签 linux bash gnu-coreutils

<分区>

出于好奇,我想知道 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,但没有什么明显的东西会导致这个变化(尽管我有一些非常模糊的感觉,我以前偶然发现过这个)。

提前致谢!

最佳答案

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

关于 bash 邮件列表的最新帖子:the questionChet's reply (2017/11).

关于linux - bash 4.3.43 和 4.4.19 之间的变量引用区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49300466/

相关文章:

linux - 如何在 Bash 脚本中使用 Expect 生成预填充的终端 session

linux - Linux 上的第一个 top 命令结果可信吗?

linux - Gnu 排序 : stray characters in field specification

linux - 将当前的 firefox/chrome URL 存储在文件中

linux - KDE 服务菜单问题

node.js - 尽管单独的 echo 语句,脚本输出被缓冲到一条消息中?

linux - 如何使用linux从子文件中读取数据并将其 append 到父文件的末尾

linux - 如何在 Linux Shell 中显示出现在连续行中的日期差异?

linux - 将文件夹内容递归移动到嵌套文件夹中

linux - 为什么 sort -u 将 U+2161 和 U+2162 视为相同的字符?