Skip to content

[exit-status] stdin 驱动的语法错误退出码丢失:未闭合引号/${ 报对消息但 rc=0(GNU 2;文件执行 rc=2 正确) #508

Closed

Description

@caomengxuan666

复现(stdin 驱动:printf '%s' "$s" | shell,5 个片段同一根因)

echo "${x:-        # F120
x=axb; echo "${x#   # F123
$(m '               # F127
echo a\necho b\n'unterminated   # F128
echo "$             # F139
片段 GNU bash 5.3.0 rubash 764b9dd
F120 bash: line 1: unexpected EOF while looking for matching }'`,rc=2 消息同(仅程序名差),rc=0
F123 同上 matching `}',rc=2 rc=0
F127 同上 matching `'',rc=2 rc=0
F128 同上 matching `'',rc=2 rc=0
F139 同上 matching `"‘,rc=2 rc=0

调用方(rubash < script || handler)靠退出码判断成败,rc=0 会让 || 分支失效。

根因(已细化:stdin 路径特有)

同一片段从脚本文件执行时 rubash rc=2 正确:

$ rubash t_eof.sh   → rc=2 ✔
$ printf '%s\n' 'echo "${x:-' | rubash   → rc=0 ✘

退出码丢失发生在 stdin(管道/重定向文件)读取路径:语法错误诊断正常打印,但顶层的 exit status 未置 2。(F181 x='abc{def'; echo \${x%%[<{(]*} 的 rc=0 同属此退出码管线,其消息形状差异另见命令位置 ( 的 issue。)

规范/源码依据

  • bash 对非交互 shell 的语法错误固定以 2 退出(bash 源码 error.h:EX_SYNTAXERR = 2;顶层解析错误路径 exit_shell(EX_SYNTAXERR))。
  • 与读取通道无关:文件与 stdin 的差异只应影响提示形式,不应影响退出码。

跨实现互证(huck)

证据

  • 语料出处:operand_dquote_context / pattern_operand_quoting / pattern_chokepoint / script_parse_error / dollar_quote_forms 五个 harness(F120/F123/F127/F128/F139)
  • 复核:764b9dde 仍复现;旧构建 41de831 行为一致

Activity

  1. added
    bug
    P1-high
    on Oct 12, 2026
  2. caomengxuan666 commented on Oct 12, 2026

    @caomengxuan666
    MemberAuthor

    Fixed and merged (see the linked PR in timeline).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P1-highbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions