Skip to content

a scalar whose text looks like a parenthesised list is re-parsed as an array by "${x[@]}" (element count and $# change) #755

Closed

Description

@caomengxuan666

Summary

A scalar whose value happens to look like a parenthesized list is re-parsed as an array when expanded with [@]. The element count changes, so "${x[@]}" — the standard idiom for "this may be a scalar or an array" — silently yields the wrong number of words.

This is not a prompt/REPL bug. It is expansion, and it looks like the same root as the S5 family the taxonomy names ("文本切片+再解析替代 parse_and_execute 一体化"): the value's text goes back through a parser.

Repro

x='(a b c)'
printf '[%s] len=%s\n' "${x[@]}" "${#x[@]}"
set -- "${x[@]}"; echo "argc=$#"

y='plain'
printf 'control:[%s] len=%s\n' "${y[@]}" "${#y[@]}"
"${x[@]}" ${#x[@]} set -- "${x[@]}" → $#
GNU bash one element (a b c) 1 1
niu / rubash three elements: a, b, c 3 3
control y='plain' one element 1 1 (both shells)

The control matches, so it triggers specifically when the scalar's text contains the parentheses.

Why this is high severity

  • "${x[@]}" is the documented way to accept "a variable that may be a scalar or an array". Downstream $#, for counts, "$@" forwarding and positional indexing all shift.
  • The trigger is ordinary data: an ls-style glob stored in a variable, a regex, a shell snippet in a config value, a log line, a find expression.
  • The failure is silent — no diagnostic, just a different word count.
  • It is not limited to the prompt path: the README of the failing case above is a plain expansion in a plain script.

Method

LF script file; niu (installed 1.4.0) and WSL GNU bash 5.3.0; stdout and stderr captured to separate files. No PTY involved.

Activity

  1. added
    bug
    P1-high
    area:expansion
    family:S1
    on Oct 17, 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

    P1-higharea:expansionbugfamily:S1

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions