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: Christoph Anton Mitterer
Subject: Re: wrong variable name in error message about unbound variable?
Date: Tue, 17 Oct 2023 04:46:22 +0200
User-agent: Evolution 3.50.0-1

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?



And a follow up question on this (guess its pointless to post this now
on help-bash):

When one just wants to check whether some var[key] is set with var
being an associative array and assuming that set -u might be in effect.

It's obviously not enough to do:
  [[ -v var[key] ]]

- in an indexed array it would do arithmetic evaluation of the key
  and might give a false positive as in:
  $ declare -a a
  $ a[3]=foo
  $ key='1+2'
  $ [[ -v a[key] ]]; echo $?
  0

- or as above, it might yield nothing but give the unbound error


One cannot use e.g ${var@a} or ${var@A} either as these would already
give unbound errors if the variable is not set (declaring it wouldn't
suffice either).

So AFAICS, the only way is to match the output of "declare -p var" for
"declare -A *" and only then to the [[ -v ... ]]?
Or would it make sense to exempt @a and @A form set -u?


Thanks,
Chris.



reply via email to

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