I am building a provisioning script that fills in variables, from where they are centrally defined in a bash file, into various non-bash files (sql, nginx, etc.) as a part of spinning up a VM.
What I need is to build an associative array thats populated with my local variables.
For example:
#!/usr/bin/env bash
var1=abc
var2=123
var3=xyz
declare -A vars_list
#magic script would produce this:
vars_list=([var1]=$var1 [var2]=$var2 [var3]=$var3)
#print out
for i in "${!vars_list[@]}"
do
echo "key : $i"
echo "value: ${vars_list[$i]}"
done
will yield:
key : var1
value: abc
key : var3
value: xyz
key : var2
value: 123
Been trying this for way too long. Seems like this is another thing that bash just cannot do simply. Help?