Bash shell script undefined array item value question

Hello,

I'm new here. I test these expressions's value in my script :
(in centOS 6 )

#!/bin/bash
array['test']='something'

echo "############"
echo ${array["test1"]}
echo ${array["test2"]}
echo ${array["5"]}
echo "############"

The output result is :

#################
something
something

#################

Why the value of ${array["test1"]} and ${array["test2"]} is "something" instead of empty ?
Because they are not defined just as ${array["5"]}, however the value of ${array["5"]} is empty and it's correct.

Thanks in advance for your help.

bash does not support associative arrays. You can only use integers as indexes.

bash do support associative arrays.
they only need to be declared:

declare -A assoAr
assoAr[subscript]="any value"
assoAr[aosubscript]="another value"
printf '%s\n' "${assoAr[@]}"
any value
another value
1 Like

I stand corrected. When was this feature intoduced?

bash 4

Thanks ! after add the declaration
declare -A array

It works as expected : The undefined item of array has empty value.