Loops used in Bash Scripts.

All the loops can be exited with break command.

For Loops

Executed on each pass for one parameter in a list.

It has the syntax:

for <variable> in <list>
do
    <do something>
done

where a <list> consists of space separated values.

A list of consecutive values from a to b can be constructed with {a..b}.

For loops can also be written in a single line:

for <variable> in <list>;do <do something>;done

While Loops

Executes as long as <condition> is true.

It has the syntax:

while [ <condition> ]
do
    <do something>
done

Until Loops

Executes until <condition> is true.

It has the syntax:

until [ <condition> ]
do
    <do something
done

Examples

For Loops

for variable in {1.4}
do
    echo $variable
done
for variable in file1 file2 file3
do
    echo $variable
done
for ip in "10.10.10.170 10.10.10.174 10.10.10.175"
do
    ping -c 1 $ip
done