Why is an empty key in a bash array always the zero key element?
Try
empty_key=
h[0]=0
h[1]=1
h[2]=2
echo ${h[$empty_key]}
Result
0
Could you please explain, why this behavior is correct?
In my understanding it should not be different to
not_existing_key=5
echo ${h[$not_existing_key]}
where the result is just empty.
This is important to understand, if one uses the result for a loop, like in
for element in ${h[$key]};do
...
done
where the empty_key leads to a cycle, but NOT the not_existing_key.
To avoid this behavior it is obviously not to start an array with index 0
OR
replace an empty key by any key value not assigned (what seems to be boring).
Again my question: why is this behavior correct?
EDIT: My question should be understood as why is this behavior the prefered one in the bash world and not an empty result as with a non-existing key value?