First page Back Continue Last page Graphics

Numeric loops: a common idiom

Numeric loops: a common idiom

You will often see, or use yourself, a simple numeric loop. This example counts up, from 0 to $max, but others may count from 1 or may count down, from $max to 0 or 1. Note that counting often starts from 0.

declare -i i=0

...

while (( i < $max ))

do

echo i has the value $i

let i++

done

This example also uses "post-incrementing". That is, the adding of 1 to i is done at the bottom of the loop. You will also see it incremented (or decremented) at the top of the loop, depending on the purpose of the script.