On Tue, Oct 17, 2023, at 10:48 AM, Christoph Anton Mitterer wrote: > 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.
No. As I said earlier, the reason "k" is not expanded here is that, for associate-array accesses, subscripts are NOT arithmetic contexts. It is not some sort of optimization. Even if all relevant variables are defined, "k" is not expanded: $ set -u $ k='1+2' $ declare -A arr=([k]=foo [1+2]=bar [3]=baz) $ echo "${arr[k]}" foo -- vq