I tried to use variable inside an array variable, but its throwing error “bad substitution”. Thanks for helping.
Example:
./xxx.ksh 1
NAME1[0]="Zara"
NAME2[0]="Daisy"
if [ $1 -eq 1 ];
then
name=NAME1
echo "First Name: ${${name}[0]}"
else
name=NAME2
echo "Second Name: ${${name}[0]}"
fi
xxx.ksh: line 7: First Name: ${$name[0]}: bad substitution
Dynamic variable names are a huge hassle. Just use a better array key:
Anyway, the bash way to achieve your goal is to use indirect expansion: