Skip to content

[parse] =~ regex word with unquoted | + reserved word (case/if) rejected in while/until condition — #322 residue (breaks shellapi odsel) #461

Closed

Description

@caomengxuan666

Construct (minimal repro)

while [[ x =~ x|case ]]; do
  echo hi
done

rubash -n (fresh master a157656, release build):

tc.sh: syntax error near unexpected token `do'

GNU bash 5.3.0 accepts this file (the entire x|case after =~ is a single regex word).

Divergence boundaries measured on the fresh build:

construct rubash -n GNU 5.3
[[ x =~ x|case ]] (top level) ok ok
if [[ x =~ x|case ]]; then ok ok
[[ x =~ ^(a|b)$ ]] (rubash#322 form) ok ok
while [[ x =~ x|zebra ]]; do ok ok
while [[ x =~ x|case ]]; do syntax error ok
until [[ x =~ x|if ]]; do syntax error ok
while [[ x =~ (a|case) ]]; do syntax error ok

Only the while/until condition context fails, and only when the segment after the unquoted | is a compound-command reserved word (case, if) — rubash then lexes case as the reserved word, opens a case command that never closes, and the error surfaces as "unexpected end of file from case' command" (or "unexpected token do'" in the minimal form).

Real-world evidence (ecosystem harvest)

irrequietus/shellapi/modules/odsel/odsel.shellapi.bash, line 59 (harvest 2026-10-04):

while [[ $y =~ ^[[:space:]]*(\||case)[[:space:]]+(.*) ]]; do
  • WSL GNU Bash 5.3.0 bash -n: rc=0 on the whole file (recorded oracle, harvest 2026-10-04/05)
  • Git Bash 5.2 grammar: rc=0 (non-authoritative corroboration)
  • rubash master a157656 rubash -n: rc=2, syntax error: unexpected end of file from 'case' command on line 59
  • Note: rubash's own runtime sources this file (ConPTY run reaches the prompt) — the strict -n parse is what breaks.

GNU C source anchor (vendored bash.git @b4608166, Bash-5.3 patch 15)

  • parse.y:5167-5170 — after the =~ operator token, the cond parser sets parser_state |= PST_REGEXP.
  • parse.y:5439-5461 read_token_word() — under PST_REGEXP, ( and | are special-cased inside the regex word: | goes to got_character (folded into the word), ( is consumed via parse_matched_pair (cd, '(', ')', ...); comment: "When parsing a regexp as a single word inside a conditional command, we need to special-case characters special to both the shell and regular expressions. Right now, that is only '(' and '|'."
  • parse.y:3663 read_token() — if (parser_state & PST_REGEXP) goto tokword; — the whole regex stays one WORD; reserved-word recognition never happens inside it.

This is a residue of the closed rubash#322 fix (2026-09-29): the ^(a|b)$ parenthesized form was fixed, but the pipe-then-reserved-word form inside while/until conditions still splits.

Activity

  1. added
    bug
    gnu-baseline
    P2-medium
    on Oct 9, 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-mediumbuggnu-baseline

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions