【问题标题】:Heredoc within command substitution: error when quotes or parentheses are unbalanced (bash and zsh)命令替换中的 Heredoc:引号或括号不平衡时出错(bash 和 zsh)
【发布时间】:2014-09-17 13:15:07
【问题描述】:

考虑以下用于打印单引号的混淆脚本,它适用于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(相关版本)中的一个错误,还是这里违反了任何语法规则?

【问题讨论】:

    标签: bash shell zsh


    【解决方案1】:

    我会认为这是一个解析错误,直到/除非开发人员另有说明。该代码在dash 中按原样工作,在this zsh question 中存在类似的未闭合引号错误。

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

    【讨论】:

    • 太糟糕了,在如此完善的 shell 中存在解析错误。 :(
    猜你喜欢
    • 1970-01-01
    • 2013-06-05
    • 1970-01-01
    • 1970-01-01
    • 2013-04-20
    • 2023-04-06
    • 2013-07-24
    • 1970-01-01
    • 2014-11-10
    相关资源
    最近更新 更多