How to iterate over an array using indirect reference?
${!ARRAYNAME[@]} means “the indices of ARRAYNAME“. As stated in the bash man page since ARRAYNAME is set, but as a string, not an array, it returns 0. Here’s a solution using eval. #!/usr/bin/env bash ARRAYNAME=’FRUITS’ FRUITS=( APPLE BANANA ORANGE ) eval array=\( \${${ARRAYNAME}[@]} \) for fruit in “${array[@]}”; do echo ${fruit} done What you were … Read more