Skip to content

[bug] eval'd compound array assignment with an extglob element is a syntax error even with extglob on (bash-completion _comp_expand_glob shape) #2876

Closed

Description

@caomengxuan666

Summary

With extglob ON, an eval'd compound array assignment whose element
carries an extglob pattern is a syntax error in rubash; GNU accepts it.
Direct (non-eval) array assignments, eval'd case patterns, and eval'd
bare glob words are all fine — only the eval × array-assignment shape
breaks, for all five extglob operators.

This is the shape bash-completion >= 2.12 hits at shell startup:
_comp_expand_glob (bash_completion line 335) runs
eval -- "$1=($2)" (line 358), and _comp_sysv_services calls it with
the pattern '"$svcdir"/!($_comp_backup_glob)'. bash-completion itself
enables extglob at load (line 45: shopt -s extglob progcomp), so the
eval must honor it.

User-visible symptom (niubash 1.5.0, Linux, interactive startup)

/usr/share/bash-completion/bash_completion: eval: line 358: syntax error near unexpected token `('
/usr/share/bash-completion/bash_completion: eval: line 358: `svcs=("$svcdir"/!($_comp_backup_glob))'

(repeated once per sysv init directory probed)

Repro (master 18277734)

# q1.sh
shopt -s extglob
for pat in '!(a)' '+(a)' '*(a)' '?(a)' '@(a|b)'; do
  eval "arr=(x/$pat)" 2>&1
  echo "eval $pat -> rc=$? arr=${arr[*]}"
done

GNU bash 5.3.20: all five rc=0, arr=x/<pat> (literal, no match in cwd).
rubash: all five print

q1.sh: eval: line 3: syntax error near unexpected token `('
q1.sh: eval: line 3: `arr=(x/!(a))'

and rc=1, arr empty.

The exact bash-completion shape also reproduces standalone:

shopt -s extglob
_comp_backup_glob='*.bak'
svcdir=/path/to/dir   # contains alpha, beta, alpha.bak
eval 'svcs=("$svcdir"/!($_comp_backup_glob))'
printf 'svcs=%s\n' "${svcs[@]}"

GNU: alpha, beta (prefixed). rubash: the eval syntax error above,
svcs empty. Same failure for eval inside a function (the
_comp_expand_glob shape), in -c strings, in script files, and for
eval inside $( ).

What passes today (scope of the defect)

  • Direct array assignment with extglob element (script or function): OK.
  • eval of a case pattern with extglob: OK (parse-time gate, see
    [bug] Code parsed by eval ignores whether extglob is enabled #1114 below).
  • eval of a bare glob word (eval "echo svcdir/!(x)"): OK.
  • trap action string containing an array assignment with an extglob
    element: OK.
  • Control: eval 'shopt -s extglob; arr=(x/!(a))' fails in BOTH shells
    (the string is parsed with the state at eval entry) — parity, not
    part of this issue.

Mechanism (read from the tree, master 18277734)

  • Executor::execute_eval_source_inner (src/executor/trap_exec.rs:1039)
    pins the process-global parse gate PARSE_EXTENDED_GLOB
    (src/lexer/mod.rs:141) to the live shopt state
    (shopt::option_enabled(env_vars, "extglob")) — but only around
    tokenize + parse; it restores the saved gate immediately after
    parse_with_options returns, before the eval'd unit executes.
  • Compound-assignment element splitting does NOT happen at parse time:
    compound_assignment_elements (src/parser/assignment.rs:38, via
    collect_compound_assignment / split_compound_assignment_words)
    reads crate::lexer::parse_extended_glob() at element-split time,
    i.e. at execution. Its own comment (assignment.rs:194-206) says the
    live gate is read at element-split time "for the execution-side
    callers".
  • So an eval'd array assignment executes after the pin has been
    restored; whenever the ambient stream gate reads off at that moment,
    the ( after a pattern operator is not consumed as a group and the
    assignment dies with the syntax error. The case parser consults
    the gate at parse time (inside the pin), which is why eval'd case
    patterns work.

GNU anchors: eval re-parses with the CURRENT parser state
(evalstring.c parse_and_execute); the lexer reads the global
extended_glob (parse.y:3502 extended_glob = extglob_flag, set from
the option at builtins/shopt.def:640) — one live value for both parse
and the execution-side pattern reads, no snapshot/restore gap.

Relation to #1114 (closed)

#1114 fixed the parse-time direction (eval ignoring the gate for
case patterns) and its pin
(tests/issue1114_eval_extglob_gate_regressions.rs) covers only that:
the OFF->reject eval and a direct post-shopt case. The ON->accept
direction through eval for compound assignments was never pinned —
this issue is that uncovered half, not a reopen of the parse-time fix.

Fix direction

Make the execution-side gate read see the state the eval was entered
with: e.g. hold the pin in execute_eval_source_inner through the
execution of the eval'd unit (not just tokenize+parse), or have the
element split consult the executor's live option state instead of the
process-global gate. Pins: eval x array x {!, +, *, ?, @}
with extglob ON (accept + correct elements) and OFF (reject), plus
the verbatim _comp_expand_glob shape above as the regression seed.

Regression seed: the q1.sh script and the _comp_expand_glob shape
above (both self-contained, GNU-verified).

Destination: 1.5.1 simple-fix class (single diagnosed mechanism).
Found by: user report from a niubash 1.5.0 interactive session loading
the distro bash-completion.

Activity

  1. added this to the 1.5.1 milestone on Nov 6, 2026
  2. added
    bug
    gnu-baseline
    area:parser
    on Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area:parserbuggnu-baseline

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions