How to iterate over an array using indirect reference?
问题 How can I make this code work? #!/bin/bash ARRAYNAME='FRUITS' FRUITS=( APPLE BANANA ORANGE ) for FRUIT in ${!ARRAYNAME[@]} do echo ${FRUIT} done This code: echo ${!ARRAYNAME[0]} Prints APPLE . I'm tryng to do something similar but with "[@]" to iterate over the array. Thanks in advance, 回答1: ${!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