Reputation: 177
Below is the code that i tried
app+=(0)
top+=(1 2 3 4 5)
for i in ${app[@]}
do
echo $i th time
app=${top[@]}
done
the output
(09:01:12)-> ./loop.sh
0 th time
please advice why cant the new value is updated and only once the code is running
Upvotes: 0
Views: 367
Reputation: 98028
I don't know what you really want to do but you can modify this to suit your needs:
app+=(0)
top+=(1 2 3 4 5)
for ((i=0; i<${#app[@]};++i)); do
echo "$i" th time, "${app[i]}" element
app=(${top[@]})
done
Gives:
0 th time, 0 element
1 th time, 2 element
2 th time, 3 element
3 th time, 4 element
4 th time, 5 element
Upvotes: 1
Reputation: 37267
In your code, ${app[@]}
is expanded only once, so it makes no different from for i in 0
. You can't modify the loop range once the loop has started. Your best bet could be using some other tricks like
ind=0
loop() {
eval $1=${app[$ind]}
(( ind++ ))
return 0
}
while loop i
do
# Now you can modify $app
done
But at last, it's not recommended to attempt to change the loop range during it.
Upvotes: 0