Comment by ๐ asdf
Re: "Name associative array after variable?"
@wasolili sorry for taking so long to respond, I've been kinda busy + I already got an answer on Reddit so the post here didn't serve much of a purpose anymore.
To answer your question, the script I'm writing takes input, and I needed to use that input as the name of an array.
I couldn't just use "declare -A array" and then use "${array[${key}]}", because the input might be something other than the word "array". The solution I got from reddit was:
declare -A $var
declare -n ref=$var
ref[$key]=$value
echo ${ref[$key]}
2025-03-01 ยท 1 year ago
Original Post
Name associative array after variable? โ I need to be able to do something like "Declare -A $var", $var["${key}"]="${value}", and echo "$var[${key}]". What would the correct syntax for this be?