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: Lawrence Velázquez
Subject: Re: wrong variable name in error message about unbound variable?
Date: Tue, 17 Oct 2023 13:53:15 -0400
User-agent: Cyrus-JMAP/3.9.0-alpha0-1019-ged83ad8595-fm-20231002.001-ged83ad85

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



reply via email to

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