How can one expand a parameter when the parameter is an array?
If the parameter is a simple variable, we can do an indirection using an exclamation point.
single_fruit()
{
if [ "$#" != 1 ]; then exit 1; fi
echo ${!1}
}
MYVAR=Persimmon
single_fruit MYVAR
I'd like to do the same on an array parameter. Rather than iterate over the elements of an array directly:
FRUIT=(Papaya Pineapple)
for f in ${FRUIT[@]}
do
echo ${f}
done
I'd like to iterate within a function:
multi_fruit()
{
if [ "$#" != 1 ]; then exit 1; fi
PARAMETER=${1}
for i in ${!PARAMETER[@]}
do
echo ${i}
done
}
MOREFRUITS=(Mango Melon)
multi_fruit MOREFRUITS
Can you make this last function iterate over the array elements?