I have a code snippet to print out an array in a shell script:
for i in "${array[@]}"; do
echo "$i"
done
}
I wanted to create a function out of it
printArray() {
for i in "${$1[@]}"; do
echo "$i"
done
}
but when I call my function with the array name (which is also available in the shell script), I get an error: ${$1[@]}: bad substitution
What I found out ist that curly braces expand first, probably trying to expand "$1[@]" literally.
I only found answers for numeric expansion like from 1 to 5. So is it possible to replace an array name with a variable inside curly braces?
I expect to be able to put a variable instead of a specific array name in my function.