1

With enabled unbound variables check (set -u), bash fails to initialize and access associative array:

during initialization

set -u
declare -a qwe=()
qwe[asd]=val # bash: asd: unbound variable

during access:

declare -a qwe=()
qwe[asd]=val
set -u
echo ${qwe[asd]} # bash: asd: unbound variable

Bash version: GNU bash, version 5.1.4(1)-release (x86_64-pc-linux-gnu)

Am I missing something or it is some kind of a bug?

1 Answer 1

5

qwe is an array, with integer indexing. When you write qwe[asd], Bash knows that asd has to be an integer, so it tries to get the value of a variable $asd (it handles the content of the square brackets as an "arithmetic context"); if it is unset, it would default to 0, but because of set -u, you get an error instead.

Did you mean declare -A for an associative array?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.