To retrieve all indices, use
${!TEST[@]}. The exclamation mark means you get a list of indices instead of a list of values, which is what
${TEST[@]} would produce. You can use either of these in a
for loop:
Code:
declare -A TEST
TEST["apple"]="Doctor"
TEST["mango"]="Summer"
TEST["pine"]="fine"
for x in ${!TEST[@]}; do
echo Index ${x} points to ${TEST[${x}]}
done
It's important to remember that the ordering of elements in an associate array is not defined. While this array obviously has three index/value pairs, they may not necessarily appear in the order they were created when you iterate through the array.
In other words, you can't ask for the index or the value of the "third" member. You'll have to look for a value corresponding to a certain index.