bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: wrong variable name in error message about unbound variable?


From: Greg Wooledge
Subject: Re: wrong variable name in error message about unbound variable?
Date: Tue, 17 Oct 2023 07:59:43 -0400

On Tue, Oct 17, 2023 at 04:46:22AM +0200, Christoph Anton Mitterer wrote:
> But why does it even try to evaluate the subscript "key" as arithmetic
> expression?

Because that's how indexed arrays work.  Everything inside the square
brackets is an arithmetic expression, and in an arithmetic context,
bash treats anything that *can* be parsed as a variable name as a
variable name.

    unicorn:~$ unset -v a b c array
    unicorn:~$ a=b b=c c=42 array[a]=foo; declare -p array
    declare -a array=([42]="foo")

> 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?

There are two undefined variables at that point, and bash chooses one
of them to report.  Prioritizing "the important error" over "the less
important error" is not something that compilers/interpreters are good
at, generally.  If you have multiple errors, you may get a report of
both of them, or just one, which masks the second one.  You'll have
to fix both of them to get the correct result anyway, so it doesn't
really matter which one gets reported and fixed first.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]