Associative arrays are local by default

后端 未结 5 1692
故里飘歌
故里飘歌 2021-02-01 14:50

Associative arrays seem to be local by default when declared inside a function body, where they should be global. The following code

#!/bin/bash

f() {
    decla         


        
5条回答
  •  天涯浪人
    2021-02-01 15:21

    You have already answered your own question with declare -g. The workaround on bash versions < 4.2 is to declare the array outside of the function.

    f() {
       map[y] = foo
    }
    
    declare -A map
    foo
    echo "${map[y]}"
    

提交回复
热议问题