On Sat, Aug 30, 2014 at 11:19 AM, lolilolicon <loliloli...@gmail.com> wrote: > 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.
It seems to be a namespace / scope problem. Another test script reveals that without assigning ref explicitly, such as using the "for ... in" construct, it behaves as expected. === script === #!/bin/bash declare -a arr=('dict[k1]' 'dict[k2]') declare -A dict='([k1]=v1 [k2]=v2)' declare -n ref for ref in "${arr[@]}"; do echo $ref done ref=${arr[0]} echo $ref ref=${arr[1]} echo $ref --- output --- v1 v2 dict[k1] dict[k2] As you see, once ref is assigned explicitly, or indeed, is assigned for a second time, its nameref attribute is lost.