Skip to content

[arith] $(( 不回退命令替换:$((echo hi) 2>&1) 算术报错 rc=1(GNU 按命令替换输出 hi rc=0) #512

Closed

Description

@caomengxuan666

复现

echo $((echo hi) 2>&1)
echo $((echo a) | tr a-z A-Z)
片段 GNU bash 5.3.0 rubash 764b9dd
F158 hi,rc=0 ...: arithmetic syntax error in expression (error token is "hi) 2>&"),rc=1
F159 A,rc=0 `...: arithmetic syntax error in expression (error token is "a)

根因

$(( 存在算术/命令替换二义性。bash 的裁决:先按算术表达式扫描,一旦确定不是算术表达式即回退按命令替换解析($((echo hi) 2>&1) ≡ $( (echo hi) 2>&1 )),输出 hi。rubash 强制按算术解析,报语法错误且 rc=1。

规范/源码依据

  • POSIX XCU 2.6.4 对 $(( 二义性的处理约定 + bash 实际行为(命令替换回退)。
  • 判据取自 GNU 实测:$((echo hi) 2>&1) 输出 hi;echo $((echo hi)) bash 同样输出 hi(同根因的无重定向形态)。

跨实现互证(huck)

huck 有同名 harness(dollar_paren_subshell,divergence 442 条之列,其契约即 ==GNU);算术行为总账见 huck#60 "Remaining arithmetic behavioral divergences (post-v216)"(open)——$(( 回退属其跟踪的算术分歧面。

证据

  • 语料出处:dollar_paren_subshell_diff_check.sh:17-18(F158/F159)
  • 复核: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.

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