Skip to content

[bug] set -u: array-subscript expansion nested in ${var#pattern} falsely reports 'unbound variable' (kills bash-completion _comp_initialize -n : class) #311

Closed

Description

@caomengxuan666

Summary

Under set -u (nounset), rubash raises a false "unbound variable" error for an array-subscript expansion nested inside the pattern of a ${var#pattern} / ${var%pattern} expansion, even though both the array element and the subscript variable are set. Without -u the same construct works. GNU bash 5.3.0 (and 5.2.21) accept it.

Found by the ecosweep2 lane driving bash-completion 2.18 core completions: _comp__reassemble_words() (bash_completion:1047 line=${line#*"${COMP_WORDS[i]}"}) dies on the first _comp_initialize -n ... call, killing every completion that excludes word-break chars (ssh-keygen, ssh, ...): rubash produces 0 replies where GNU produces 59 for ssh-keygen -.

Minimal reproducer (script file, run on both shells)

set -u
A=(x0 x1)
i=0
r=abc
r=${r#*"${A[i]}"}
echo "[$r]"
  • GNU 5.3.0 (wsl /usr/local/bin/bash): prints [abc], rc=0
  • rubash (release build @116b12aa): line 5: r#*"${A[i]: unbound variable, rc=1

Variants probed (all crash on rubash, all pass on GNU):

  • subscript local to the function, array global; subscript global, array local; both local — scope does not matter
  • same construct without set -u — works on rubash (nounset is the trigger)
  • plain ${A[i]} outside a pattern context under set -u — works on rubash

GNU C source anchors (vendored third_party/bash, bash.git @b4608166)

  • subst.c:10320 case '#': /* ${param#[#]pattern} */ — the pattern (value) reaches this case already expanded as an ordinary nested word; GNU does not apply any stricter boundness rule to expansions nested in a pattern.
  • subst.c:7663 parameter_brace_expand_word() (call sites 7861/7955) — the nested ${A[i]} resolves through the normal array-element lookup; with A[0] set there is no unbound condition (unbound reporting for subscripts guarded at subst.c:7500-7510, only when the var is invisible/absent).

Impact

Environment

  • rubash: release build of 116b12a (wt13/ecosweep2 worktree), Windows
  • Oracle: WSL GNU bash 5.3.0 /usr/local/bin/bash (plus 5.2.21 cross-check), script-file runs
  • Artifacts: target/issue-suites/results/ecosweep2/ p-nested2.sh, p-nested3.sh, p-faithful.sh, p-trace.sh + {rb,gnu}*.out/.err/.rc, xtrace log

Activity

  1. added
    bug
    on Sep 29, 2026
  2. caomengxuan666 commented on Sep 29, 2026

    @caomengxuan666
    MemberAuthor

    Family widened — ecosweep2 lane (liquidprompt own test suite). The same false-nounset fires on a much larger shape class than the original nested-array report. Isolated per-shape matrix (each case in its own subshell, set -u active, all vars set; GNU 5.3.0 passes every case with rc=0, rubash release @116b12aa rc=1 on the marked ones):

    shape example rubash
    # + bracket class ${line#[[:space:]]} CRASH line#[[:space:]]: unbound variable
    % + bracket class ${line%[[:space:]]} CRASH
    # + negated bracket (escaped /) ${p2#[!\/]} CRASH
    // + bracket class ${s2//[[:alpha:]]} CRASH
    // + plain bracket ${s2//[a,]} CRASH
    // + negated bracket ${p2//[!\/]}, ${s2//[!/]"} CRASH
    # + nested quoted array elem ${s2#*"${A[i]}"} CRASH (original report)
    # + literal ${line#x} ok
    %% + bracket class + * ${line%%[[:space:]]*} ok
    // + single char (+repl) ${s2//,}, ${s2//,/;} ok
    / single + bracket + repl ${s2/[a]/X} ok

    Common symptom: the unbound diagnostic prints the whole expansion text as the variable name (q//[!\/]: unbound variable), i.e. the nounset check's name extraction is consuming the pattern. Without set -u every shape works, so the parse itself is fine — only the nounset path mis-extracts.

    Blast radius (liquidprompt @afd7e83 own suite, ecosweep2): liquidprompt:1736 local slash_count="${display_path//[!\/]}" kills test_tools and test_bash_preexec; ${line#[[:space:]]}-class kills test_python_venv. Plus the bash-completion class from the original report.

    Artifacts: target/issue-suites/results/ecosweep2/ p-shapes.sh + rb-shapes.out/gnu-shapes.out (full matrix), p-negclass*.sh, liquidprompt suite outputs under lp/rb/.

  3. added 2 commits that reference this issue on Sep 29, 2026
  4. caomengxuan666 commented on Sep 29, 2026

    @caomengxuan666
    MemberAuthor

    Fixed by wt13/sweepfix (a6f9664a, master 203c2b5): nounset_braced_parameter_is_unbound fed the whole ${...} body to parse_array_subscript - bracket classes in patterns ([[:space:]]) were parsed as subscripts (line#[[:space:]] -> base line#, sub [:space:]) producing false unbound plus the whole body as the diagnostic name. New parameter_reference_len mirrors string_extract operator delimiting (subst.c:9807, :791, :812-818); operator tails checked per operator position (default/assign/alternate/all-elements bails); nounset_variable_bound now consults the typed store (array_p/assoc_p cell semantics). Family follow-ons fixed in the same pass: operator-tail diagnostic names, -/=/+ swallowed inside patterns, @-transform misses, and the text-heuristic is_array_storage misreading the literal () value. 8-shape issue matrix + 30-shape family matrix byte-identical on 5.3.0 AND 5.2.21; the old binary dies in set -u on ssh-keygen completion, the new one produces 59 replies byte-identical to GNU; three liquidprompt test files green again.

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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions