First of all sorry because my english may be not good. I want to use a variable to index an element in an array or use the same variable to index all the elements. For example:
...
var1="1"
var2="*"
array=(one two three for five)
for elem in ${array[$var1]}
do
echo $elem
done
When I use var1 to index in ${array[$var1]} it works correctly, but if I use var2 doesn't work correctly, I get this error:
./ed.sh line XXX *: syntax error: operand expected (error token is "*")
I'm pretty sure that the error is related with the * wildcard expansion, but I didn't find an answer that help me to solve this problem. So, how can I do it?
*, because non-associative arrays only have integers as keys.${array[*]}is valid for indexed arrays, it displays all the elements of the array.${!array[*]}displays all the indexes that have values, which can be useful because these arrays are sparse. The problem here is the expansion order, which evenevaldoes not appear to fix.