I have a script that iterates over an array of values, something like this (omitted for the purpose of this question):
COUNTRIES=( ENGLAND SCOTLAND WALES )
for i in ${COUNTRIES[@]}
do
echo "Country is $i "
done
My question is: is it possible to replace the array dynamically? For example, I want to be able to pass an iteration in an array at runtime. I tried the following, but I think my syntax might be wrong
COUNTRIES=( ENGLAND SCOTLAND WALES )
ANIMALS=( COW SHEEP DOG )
loopOverSomething()
{
for i in ${$1[@]}
do
echo "value is $i "
done
}
loopOverSomething $ANIMALS
I get line 22: ${$2[@]}: bad substitution
Jimmy source
share