On Mon, Oct 16, 2023, 22:46 Christoph Anton Mitterer <cales...@scientia.org> wrote:
> Hey. > > On Mon, 2023-10-16 at 22:05 -0400, Lawrence Velázquez wrote: > > Under no circumstances should your examples complain about "array" > > because they do not attempt to expand it. As I demonstrated, your > > examples do not even complain about unset scalar variables. > > Okay I realise now, why it worked once it was declared an associative > array. > > But why does it even try to evaluate the subscript "key" as arithmetic > expression? > > Yes it's defined for indexed arrays, but shouldn't it already know that > there is no indexed array of the name "array" and any evaluation of the > subscript is thus pointless? > 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: $ unset a b; echo "${a[b=2]-X}" "$b" X 2 Though I do think Bash isn't being very consistent in the case where a side effects of expanding a subscript of an unset variable ends up making it no longer unset: $ unset a; echo "${a[a=0]-X}" "$a" X 0 $ unset a; declare a; echo "${a[a=0]-X}" "$a" 0 0