gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 09:31:57 26 4
gpt4 key购买 nike

考虑以下用于打印单引号的混淆脚本,它适用于 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com