The for loop

[user@host ~]$ for i in first second third
> do
> echo $i iteration
> done
first iteration
second iteration
third iteration

We have defined a loop in which the variable “i” will take the values “first”, “second” and “third” in that order.

do/done - denote the start and end of the loop iteration definition.

We can also use increments in our for loop definitions:

[user@host ~]$ for (( i=1 ; i<=5 ; i++ ))
> do
> echo iteration$i
> done
iteration1
iteration2
iteration3
iteration4
iteration5

At each iteration the statement incrementi is printed and i is incremented by 1, until i = 6 and the conditions of the for loop are no longer met and the loop ends.

Next activity: Exercises - Variables and Loops.