Reputation: 25
I'm trying to loop through an array and update the element at the i'th index. I've tried several ways but they've all resulted in an error.
Here I'm trying to increment each element by one.
TEST_ARR=(0 1 2 3 4 5 6 7 8 9)
for (( i=0; i<${#TEST_ARR[@]}; i++ )); do
${TEST_ARR[$i]}++
done
Here I'm trying to set each element to a different value.
TEST_ARR=(0 1 2 3 4 5 6 7 8 9)
for (( i=0; i<${#TEST_ARR[@]}; i++ )); do
${TEST_ARR[$i]}=0
done
Upvotes: 1
Views: 40
Reputation: 781706
You need to put arithmetic expressions inside (())
.
When you're assigning to a variable, you don't put $
before it.
for (( i=0; i<${#TEST_ARR[@]}; i++ )); do
((TEST_ARR[$i]++))
done
for (( i=0; i<${#TEST_ARR[@]}; i++ )); do
TEST_ARR[$i]=0
done
Upvotes: 3