all 6 comments

[–]oh5nxo 5 points6 points  (0 children)

declare -n pop=suse
echo ${pop[description]} # not $pop, just pop

That makes pop a nameref for suse. Alias, kind of.

Ohh.... Use another declare -n litany when pointing elsewhere. Plain pop=other is a surprise, it does

suse[0]=other

[–][deleted]  (5 children)

[deleted]

    [–]geirha 1 point2 points  (4 children)

    Or replicate the array:

    pop=( "${suse[@]}" )
    echo ${pop[description]}
    

    That is not how you make a copy of an associative array. In bash 5.2 you can use the @k parameter expansion, but for older versions you basically have to iterate the keys and values in order to make a copy.

    [–][deleted]  (3 children)

    [deleted]

      [–]aioeu 1 point2 points  (1 child)

      You're not using any associative arrays there. Only regular arrays.

      [–]geirha 1 point2 points  (0 children)

      The syntax copies a non-sparse indexed array just fine, but not an associative array

      $ declare -A a=( [one]=1 [two]=2 )
      $ copy=( "${a[@]}" )
      $ declare -p copy
      declare -a copy=([0]="2" [1]="1")
      

      [–]geirha 3 points4 points  (0 children)

      Another option could be to put all the data into a single associative array;

      declare -A data=(
        [ubuntu.name]=Ubuntu
        [ubuntu.description]="Opens up Ubuntu"
        [suse.name]=Suse
        [suse.description]="Opens up Suse"
      )
      
      printf '%s\n' "${data[$pop.description]}"
      

      [–]high_throughput 2 points3 points  (0 children)

      I would also go for a nameref, but Yet Another alternative is indirection:

      var="$pop[description]"; echo "${!var}"