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: Mon, 16 Oct 2023 22:05:50 -0400
User-agent: Cyrus-JMAP/3.9.0-alpha0-1019-ged83ad8595-fm-20231002.001-ged83ad85

On Mon, Oct 16, 2023, at 9:06 PM, Christoph Anton Mitterer wrote:
> $ set -u
> $ [ -n "${array[key]+is_set}" ] && echo is set || echo not set
> bash: key: unbound variable
> $ [[ -v array[key] ]] && echo is set || echo not set
> bash: key: unbound variable

Since "array" has not been declared at this point, "${array[key]}"
attempts to access an indexed array named "array".  The shell
evaluates the subscript as an arithmetic expression and tries to
expand a variable named "key", which is unset.  Hence the error.


> $ declare -A array
> $ [ -n "${array[key]+is_set}" ] && echo is set || echo not set
> not set
> $ [[ -v array[key] ]] && echo is set || echo not set
> not set

Once you declare "array" as an associative array, the subscript is
no longer evaluated as an arithmetic expression -- "key" is just
the string k-e-y.

There are no further errors because these examples do not actually
attempt to expand an unset variable.  Observe the same behavior
with a scalar variable:

        bash-5.2$ set -u
        bash-5.2$ unset var
        bash-5.2$ [ -n "${var+set}" ]
        bash-5.2$ [[ -v var ]]

Conversely, if you actually do try to expand "${array[key]}", you
will observe an error.

        bash-5.2$ set -u
        bash-5.2$ unset array
        bash-5.2$ declare -A array
        bash-5.2$ : "${array[key]}"
        bash: array[key]: unbound variable

set -u is about *expanding* unset variables, not merely accessing
them.


> Once array is declared (not even anything of it set yet) it gives
> already no unbound error.

This is a misdiagnosis.  Observe that declaring "array" as an indexed
array still leads to errors about "key".

        bash-5.2$ set -u
        bash-5.2$ unset array
        bash-5.2$ declare -a array
        bash-5.2$ [ -n "${array[key]+set}" ]
        bash: key: unbound variable
        bash-5.2$ [[ -v array[key] ]]
        bash: key: unbound variable

The lack of errors is not caused by declaring "array" in general,
but specifically by declaring it as an *associative array*, and
then using it in contexts that never throw set -u errors.


> So shouldn't the error message refer to "array" as the unbound
> variable? Or perhaps both "array[key]"?

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.


> Because the problem isn't that no key named "key" exists.

Yes, that is the problem.


> And beyond that:
> POSIX says:
>> -u
>>    When the shell tries to expand an unset parameter other than the
>>    '@' and '*' special parameters, it shall write a message to
>>    standard error and the expansion shall fail with the consequences
>>    specified in Consequences of Shell Errors.
>
> But just declaring the var, doesn't make it set... so shouldn't it
> still give an error message (which it does when using a non-array
> variable that is declared but not set)?

As I demonstrated, attempting to expand "array" using an nonexistent
key does produce an error message.


-- 
vq



reply via email to

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