The following is my test scripts and their output. They are very similar, and I do not think they should result in different output. The output I expected from all of them is:
v1 v2 But as you can see, only 1 in the following 4 cases does it meet my expectation. In the other 3 cases, the nameref attribute of the variable 'ref' is lost unexpectedly. === script === #!/bin/bash declare -a arr=('dict[k1]' 'dict[k2]') declare -A dict='([k1]=v1 [k2]=v2)' func() { local -i i for ((i=0; i<${#arr[@]}; ++i)); do local -n ref ref=${arr[i]} echo $ref done } func --- output --- v1 v2 === script === #!/bin/bash declare -a arr=('dict[k1]' 'dict[k2]') declare -A dict='([k1]=v1 [k2]=v2)' func() { local -i i local -n ref for ((i=0; i<${#arr[@]}; ++i)); do ref=${arr[i]} echo $ref done } func --- output --- v1 dict[k2] === script === #!/bin/bash declare -a arr=('dict[k1]' 'dict[k2]') declare -A dict='([k1]=v1 [k2]=v2)' declare -i i for ((i=0; i<${#arr[@]}; ++i)); do declare -n ref ref=${arr[i]} echo $ref done --- output --- v1 dict[k2] === script === #!/bin/bash declare -a arr=('dict[k1]' 'dict[k2]') declare -A dict='([k1]=v1 [k2]=v2)' declare -i i declare -n ref for ((i=0; i<${#arr[@]}; ++i)); do ref=${arr[i]} echo $ref done --- output --- v1 dict[k2]