Bash indirect reference to an associative array

后端 未结 3 1240
醉酒成梦
醉酒成梦 2020-12-18 07:31

In this very simplified example, I need to address both key and value of an array element:

declare -A writer
writer[H.P.]=Lovecraft
writer[Stephen]=King
writ         


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-18 08:22

    From the Bash Reference Guide:

    The positional parameters are temporarily replaced when a shell function is executed (see Shell Functions).

    So you could do this:

    fullname()
    {
        for first
        do
            echo "$first ${writer[$first]}"
        done
    }
    fullname "${!writer[@]}"
    

提交回复
热议问题