How to pass an associative array as argument to a function in Bash?

Posted by niksfirefly on Stack Overflow See other posts from Stack Overflow or by niksfirefly
Published on 2010-11-01T13:16:53Z Indexed on 2012/06/27 9:16 UTC
Read the original article Hit count: 265

How do you pass an associative array as an argument to a function? Is this possible in Bash?

The code below is not working as expected:

function iterateArray
{
    local ADATA="${@}"            # associative array

for key in "${!ADATA[@]}"
do
    echo "key - ${key}"
    echo "value: ${ADATA[$key]}"

done

}

Passing associative arrays to a function like normal arrays does not work:

iterateArray "$A_DATA"

or

iterateArray "$A_DATA[@]"

© Stack Overflow or respective owner

Related posts about arrays

Related posts about bash