I tried to use variable inside an array variable, but its not working as expected.
CODE
ENV2[0]=567
ENV1[0]=123
ENV1[1]=789
if [ $1 -eq 1 ]
then
name=ENV1
echo "${name[0]}"
echo "${name[1]}"
else
name=ENV1
echo "${name[1]}"
fi
Output: ENV1
Instead of “123”, its printing “ENV1” and a blank line in second echo part. Please help to get the correct output and i am a beginner. Thanks
name=ENV1will not assign the content of the variableENV1to variablename, but the actual string ENV1 instead.To copy the array
ENV1toname, you could use this:This is same as
name=${ENV1[0]}. The first element of the arrayvariable will be copied, because the index is not specified. So here,
only the index 0 is considered.
To assign all values of
ENV1toname, you should use this:This sets the array variable
nameto all elements ofENV1.