0

here is a sample code

astr[10]=a
astr[20]=b
astr[30]=b
astrR="astr[@]"
echo ${!astr[@]} #the indexes
echo ${!astrR} #the values
#FAIL: echo ${!!astrR}

astrR="!astr[@]" # wild guess
echo ${!astrR} # empty output, so I am still wild

So, how to get the indexes using astrR ?

  • I found no question like this one, but I actually just found a way to answer it here: https://unix.stackexchange.com/a/390763/30352, so I thought was good to keep this question. – Aquarius Power Jan 21 '19 at 15:02
  • I'm not quite sure what you are trying to do with astrR? Are you trying to print indices and values of the associative array? – Inian Jan 21 '19 at 16:56
  • Can you explain the use-case you are trying to solve? – Inian Jan 21 '19 at 17:08
  • I get the array name as a function param, then I need to modify the external array using the reference and keeping the indexes, for that I needed a way to access the external array by it's name (as a not known variable) to maintain it (add, remove items etc) – Aquarius Power Jan 21 '19 at 22:58
  • Expansions with references in bash is not supported directly. You might need to re-consider your design if you are planning to do this bash – Inian Jan 22 '19 at 02:56
  • it is already working: declare -n strR="astr", may be I should just delete this? – Aquarius Power Jan 22 '19 at 16:02

1 Answers1

0

declare -n astrR="astr"

from tips here: https://unix.stackexchange.com/a/390763/30352

(answered mainly to not be left as unanswered and calling unnecessary attention from other questions needing it)