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

后端 未结 8 2200
自闭症患者
自闭症患者 2020-11-27 03:50

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 iter         


        
8条回答
  •  误落风尘
    2020-11-27 04:53

    From the best Bash guide ever:

    declare -A fullNames
    fullNames=( ["lhunath"]="Maarten Billemont" ["greycat"]="Greg Wooledge" )
    for user in "${!fullNames[@]}"
    do
        echo "User: $user, full name: ${fullNames[$user]}."
    done
    

    I think the issue in your case is that $@ is not an associative array: "@: Expands to all the words of all the positional parameters. If double quoted, it expands to a list of all the positional parameters as individual words."

提交回复
热议问题