On 6/30/25 5:06 AM, Isabella Bosia wrote:
this causes a parser error since commit 35bc7025
a=(0 1 2)
b=(0 1 2)
x=0
((i=$x,a[b[i]]))
using $ anywhere in (( )) makes subscripts not expandable
Not quite true; it expands them only once. Here's what I mean.
The arithmetic expression within (( and )) is equivalent to
let "expression"
so the contents are expanded once as if in double quotes. This has
always been the case -- earlier versions of bash just translated
((expr)) into let "expr" internally, and the documentation was explicit
about the equivalence.
That turns this expression into i=0,a[b[i]]
since the contents of the outer array subscript (b[i]) aren't modified by
word expansion. Now, we don't want this to be expanded again, so we
internally quote the characters in the subscript value that would cause it
to be expanded (it also helps to find the end of the subscript later,
since the quotes that might have helped determine this are removed as part
of word expansion).
If the expression hadn't contained $x, nothing would have been expanded,
because expanding a word without any expansion characters is a waste of
time. This arguably causes a problem in this case; it would have been
more consistent to run the whole string through word expansion and quote
the subscript in all cases.
You can't know with certainty whether you're dealing with an indexed or
associative array at this point, since the array might have been created
earlier in the expression, or may not exist at all. That determination has
to happen when the expression is evaluated.
It's clear that the problem here is this internal quoting not being removed
at the appropriate time. I'm just not sure what to do about it yet.
Note that you can make this work by deferring all expansions to expression
evaluation time (removing the `$') because the above word expansions won't
be performed.
Chet
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU c...@case.edu http://tiswww.cwru.edu/~chet/