Skip to content

[cmdsub] $() 注释内的 ) 提前终止替换(GNU 注释延伸到行尾,输出 [hi]) #513

Closed

Description

@caomengxuan666

复现

echo "[$(echo hi  # c with ) paren
)]"
stdout rc
GNU bash 5.3.0 [hi] 0
rubash 764b9dd [hi paren + 换行 + )] 0

根因

$( ... ) 体内的 # 注释延伸到行尾,注释中的 ) 不应终结替换。rubash 在扫描替换终结符时未跳过注释,被注释内的 ) 提前截断:$(echo hi # c with ) 被当成替换体,剩余 paren\n)] 按普通文本输出。

规范/源码依据

  • XCU 2.3 Token Recognition:# 开启注释至行尾,注释内容不参与 token 化——该规则在 $( ) 扫描域内同样适用。
  • bash 源码 parser(parse.y read_token 路径):$( 体扫描中注释由 lexer 统一吞掉,]/) 只有在 token 位置才参与配对。

跨实现互证(huck)

huck#109 "Comment inside $(...) body mis-parsed as syntax error (comment-after-open / comment-only body)"(已修复)——同一语义点(cmdsub 体对注释的处理),huck 修复覆盖注释后内容/纯注释体;本例(注释内的 ))是其对偶表现,huck 修复后同样正确。

证据

  • 语料出处:cmdsub_comment_diff_check.sh:14(F049,label: paren in comment)
  • 复核:764b9dde 仍复现;旧构建 41de831 行为一致

Activity

  1. added
    bug
    P2-medium
    on Oct 12, 2026
  2. caomengxuan666 commented on Oct 12, 2026

    @caomengxuan666
    MemberAuthor

    Fixed by the merged Wave batch (#576 for #512/#513, #569 for #550). RED-then-GREEN regression evidence in the PR.

  3. added a commit that references this issue on Oct 20, 2026
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

    P2-mediumbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions