Loops for, while and until

For loop

#!/bin/bash
for i in `seq 1 10`;
do
        echo $i
done
#!/bin/bash
for i in `ls`;
do
        echo $i
done

Warning

The for loops over words and not items as other languages. Files and/or directories with spaces will be split into multiple items.

#!/bin/bash
for i in `$( ls )`;
do
        echo $i
done

While loop

The while loop executues a command as long as the control expression is true, and stops when it becomes false or when a break statement is executed.

#!/bin/bash
COUNTER=0
while [  $COUNTER -lt 10 ]; do
    echo The counter is $COUNTER
    let COUNTER=COUNTER+1
done

Until loop

The until loop is similar to the while loop, but the condition is inverted. Meaning that the loop will execute as long as the condition is false.

#!/bin/bash
COUNTER=20
until [  $COUNTER -lt 10 ]; do
    echo COUNTER $COUNTER
    let COUNTER-=1
done