bash - 命令替换中的 Heredoc : error when quotes or parentheses are unbalanced (bash and zsh)

标签 bash shell zsh

考虑以下用于打印单引号的混淆脚本,它适用于 ksh:

#!/bin/ksh
echo "$(cat <<EOF
'
EOF
)"

输出:

'

但是,当我在 OS X 上使用 bash 3.2.51(1) 运行相同的脚本时,

#!/bin/bash
echo "$(cat <<EOF
'
EOF
)"

bash 报告以下错误:

./heredoc-within-cmdsubst: line 3: unexpected EOF while looking for matching `''
./heredoc-within-cmdsubst: line 6: syntax error: unexpected end of file

并使用 zsh 5.0.2 运行,

#!/bin/zsh
echo "$(cat <<EOF
'
EOF
)"

zsh报如下错误:

./heredoc-within-cmdsubst:6: unmatched "

将单引号替换为双引号或圆括号时会出现类似的错误。如果我用匹配的单引号/双引号/括号来平衡单引号/双引号/括号,那么脚本可以在 bash 和 zsh 上正常运行。

这个问题只是 bash 和 zsh(相关版本)中的一个错误,还是这里违反了任何语法规则?

最佳答案

除非开发人员另有说明,否则我会认为这是一个解析错误。该代码按原样在 dash 中运行,并且在 this zsh question 中存在类似的未闭合引号错误.

更新:这实际上已在 bash 4.1 中修复;我只 checkin 了zsh 5.0.2(最新版本是5.0.6)

关于bash - 命令替换中的 Heredoc : error when quotes or parentheses are unbalanced (bash and zsh),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25891644/

相关文章:

bash - 如果列值为空,则 awk 打印空

regex - Bash 脚本 - 需要帮助才能使匹配和替换正常工作

linux - 使用 find 命令 rsync 备份文件到远程服务器

linux - 如何为行编写 while 循环

c - 自动为执行的程序提供输入

sql - 无法在 shell 命令中工作的情况

intellij-idea - .zshrc 未源自 Intellij 中加载的 tmux

ruby - rvm 一直忽略我的 .rvmrc

linux - which 命令在 zsh 和 bash 中的不同行为

bash - 如何循环遍历大文件和输出行以通过 stdin 进行 curl ?