Bash scripting - Iterating through "variable" variable names for a list of associative arrays

Posted by user1550254 on Stack Overflow See other posts from Stack Overflow or by user1550254
Published on 2012-11-09T09:54:00Z Indexed on 2012/11/09 11:01 UTC
Read the original article Hit count: 205

Filed under:
|
|
|

I've got a variable list of associative arrays that I want to iterate through and retrieve their key/value pairs.

I iterate through a single associative array by listing all its keys and getting the values, ie.

for key in "${!queue1[@]}" do
    echo "key : $key"
    echo "value : ${queue1[$key]}"
done

The tricky part is that the names of the associative arrays are variable variables, e.g. given count = 5, the associative arrays would be named queue1, queue2, queue3, queue4, queue5.

I'm trying to replace the sequence above based on a count, but so far every combination of parentheses and eval has not yielded much more then bad substitution errors. e.g below:

for count in {1,2,3,4,5} do
    for key in "${!queue${count}[@]}" do
        echo "key : $key"
        echo "value : ${queue${count}[$key]}"
    done
done

Help would be very much appreciated!

© Stack Overflow or respective owner

Related posts about linux

Related posts about bash