On Tue, 2023-10-17 at 00:26 -0400, Grisha Levit wrote: > The array subscript can an arbitrary arithmetic expression with side > effects, so it makes sense to perform the expansion even if the array > whose subscript is being expanded is unset:
Okay... that's all pretty convoluted. I assume it boils down to the idea that a variable that's not an associative array may become automatically an indexed array, right? As Lawrence pointed out: $ set -u $ declare -A a $ echo ${a[k]} bash: a[k]: unbound variable Here it actually looks first at a (which turns out to be an associative array) and thus doesn't even bother to evaluate k, which makes sense of course, in order to avoid any side effects. Here, when a is still undeclared it still makes "sense" to evaluate the subscript, as a may even become a declared indexed array while doing so: $ set -u $ declare -p a bash: declare: a: not found $ echo ${a[a[4]=0]} bash: a[a[4]=0]: unbound variable $ declare -p a declare -a a=([4]="0") okay, makes sense to me. But: $ set -u $ declare -p a bash: declare: a: not found $ echo ${a[a[4]=4]} bash: a[a[4]=4]: unbound variable $ declare -p a declare -a a=([4]="4") Why does it say unbound here? a[4]=4 should evaluate to 4 and also set the index 4, right? Cheers, Chris.