[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: nounset option: Error message points to the wrong variable when acce
From: |
Emanuele Torre |
Subject: |
Re: nounset option: Error message points to the wrong variable when accessing associative arrays |
Date: |
Wed, 10 May 2023 17:37:41 +0200 |
User-agent: |
Mutt/2.2.10 (2023-03-25) |
On Wed, May 10, 2023 at 05:25:35PM +0200, Emanuele Torre wrote:
> On Wed, May 10, 2023 at 02:07:46PM +0000, Baumann, Moritz wrote:
> > Description:
> > When the nounset option is set, and you try to access a key of an
> > associative array that does not exist, the error message complains
> > about the key being unbound instead of the array variable.
>
> That is correct behaviour: you set myarray[foo], but you tried to expand
> my_array[foo] that is not set.
>
> emanuele6
Oh, I am sorry; I should have read your description more carefully.
Anyway, that is still correct behaviour; the reason why that happens is
that when you assign you perform an array expansion on a variable that
is neither a -a nor a -A variable, the variable is treated as a -a
(indexed array) variable.
Indexed array variables evaluate their subscripts as artihmetic
expressions:
$ bar=1
$ arr=( a b c )
$ printf %s\\n "${arr[bar + 1]}"
c
$ printf %s\\n "${arr[foo]}" # foo is unset and defaults to 0
a
$ set -u
$ printf %s\\n "${arr[foo]}" # foo is unset and defaults to 0
bash: foo: unbound variable
$ printf %s\\n "${arr[4]}"
bash: abc[4]: unbound variable
So bash is reporting that the variable foo does not exist because the
code tried to expand it, not because it reported the key instead of the
array name.
If arr were an -A (associative array) variable, this would not happen
because foo would just be a string
$ declare -A xyz
$ echo ${xyz[foo]}
bash: xyz[foo]: unbound variable
When you expand a non -A variable, the subscript will always evaluated
before identifier[resultofsubscript].
Also note that nounset does not report errors if you expand an array
variable that is not set with @, or a [@]:start:length PE that does not
have values, that is why the subscript is evaluated first.
$ set -u
$ echo "${foo[@]}"
$ echo "${foo[@]:2:3}"
$ echo "${foo[2]}"
bash: foo[2]: unbound variable
emanuele6
- nounset option: Error message points to the wrong variable when accessing associative arrays, Baumann, Moritz, 2023/05/10
- Re: nounset option: Error message points to the wrong variable when accessing associative arrays, Emanuele Torre, 2023/05/10
- Re: nounset option: Error message points to the wrong variable when accessing associative arrays,
Emanuele Torre <=
- RE: nounset option: Error message points to the wrong variable when accessing associative arrays, Baumann, Moritz, 2023/05/10
- Re: nounset option: Error message points to the wrong variable when accessing associative arrays, Emanuele Torre, 2023/05/10
- RE: nounset option: Error message points to the wrong variable when accessing associative arrays, Baumann, Moritz, 2023/05/10
- RE: nounset option: Error message points to the wrong variable when accessing associative arrays, Baumann, Moritz, 2023/05/11
- Re: nounset option: Error message points to the wrong variable when accessing associative arrays, Emanuele Torre, 2023/05/11
- Re: nounset option: Error message points to the wrong variable when accessing associative arrays, alex xmb ratchev, 2023/05/10
Re: nounset option: Error message points to the wrong variable when accessing associative arrays, Andreas Schwab, 2023/05/10