First of all, sorry, because my English may not be very good. I want to use a variable to index an element in an array or use the same variable to index all elements. For instance:
...
var1="1"
var2="*"
array=(one two three for five)
for elem in ${array[$var1]}
do
echo $elem
done
When I use var1 to index into $ {array [$ var1]} , it works correctly, but if I use var2, it does not work correctly, I get this error:
./ed.sh line XXX *: syntax error: operand expected (error token is "*")
I am sure that the error is related to the expansion of substitution *, but I did not find an answer that will help me solve this problem. So how can I do this?
source
share