命令替换中的 Heredoc:引号或括号不平衡时出错(bash 和 zsh) [英] Heredoc within command substitution: error when quotes or parentheses are unbalanced (bash and zsh)

查看:33
本文介绍了命令替换中的 Heredoc:引号或括号不平衡时出错(bash 和 zsh)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Consider the following obfuscated script intended for printing a single quote, which works on ksh:

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

输出:

'

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

However, when I run the same script with bash 3.2.51(1) on OS X,

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

bash 报告以下错误:

bash reports the following errors:

./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 中正常运行.

Similar errors occur when the single quote is replaced by a double quote or parenthesis. If I balance the single quote/double quote/parenthesis with a matching single quote/double quote/parenthesis, then the script runs fine with both bash and zsh.

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

Is this issue just a bug in (the relevant versions of) bash and zsh, or are there any syntactic rules violated here?

推荐答案

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

I would consider this a parsing bug until/unless the developers say otherwise. The code works in dash as-is, and there is a similar unclosed-quote error in this zsh question.

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

UPDATE: This is actually fixed in bash 4.1; I only checked in zsh 5.0.2 (the latest version is 5.0.6).

这篇关于命令替换中的 Heredoc:引号或括号不平衡时出错(bash 和 zsh)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆