bash
evaluates the following expression without any objections:
declare -A SPANISH=( [rojo]=red [verde]=green [azul]=blue )
...but it does not like this one one bit:
declare -A SPANISH=( $( echo "[rojo]=red [verde]=green [azul]=blue" ) )
bash: SPANISH: $( echo "[rojo]=red [verde]=green [azul]=blue" ): must use subscript when assigning associative array
I have tried many variations, but the result is always one or more instances of the must use subscript when assigning associative array
error shown above.
This example is silly, of course. Its only purpose is to illustrate the problem of initializing a bash associative array using a command substitution.
Can this be done?
To be concrete, and to continue with the silly example above, what would be a command <COMMAND>
such that
declare -A SPANISH=( $( <COMMAND> ) )
...produces the same final result as that produced by
declare -A SPANISH=( [rojo]=red [verde]=green [azul]=blue )
above?
Incidentally, zsh
handles this situation without any problem:
$ declare -A SPANISH=( $( echo "rojo red verde green azul blue" ) )
$ echo "${SPANISH[azul]}"
blue
[rojo]=red [verde]=green [azul]=blue
.