How to use variable inside array?

I tried to use variable inside an array variable, but its not working as expected.:wall:

 
ENV1[0]=123
ENV1[1]=789
ENV1[2]=120
ENV2[0]=567
if [ $1 -eq 1 ]
then
name=ENV1
echo "${name[0]}"
echo "${name[1]}"
echo "${name[2]}"
else
name=ENV1
echo "${name[1]}"
fi
 

Output: ./val.sh 1
123
<blank line>

123 is for "${name[0]}" and blank line is for "${name[1]}". I want 789 for "${name[1]}" and 120 for "${name[2]}" as output.

Please help to get the correct output and i am a beginner. Thanks

What shell are you using? I'd rather think name=ENV1 assigns the string constant "ENV1" to the scalar variable name , so the 123 output is a bit surprising to me... possible that the array name has been assigned to earlier?

I got the solution by using:cool:

Set -A name ${ENV1[@]}
ENV1[0]=123 ENV1[1]=789 ENV1[2]=120 ENV2[0]=567 if [ $1 -eq 1 ] then Set -A name ${ENV1[@]} echo "${name[0]}" echo "${name[1]}" echo "${name[2]}" else name=ENV1 echo "${name[1]}" fi

Refer: eTechTricks.com