Loops

Loops are used for automating repetitive tasks and continue to run until a specific condition is met.

 

The main loop structures in shell scripting are:

 

  • for
  • while

 

Syntax of a for loop:

 

for value in source
do

command

done

 

*where source can be:

  • a number of separately listed items
  • a variable
  • an array
  • a substituted command e.g. $(command)

 

 

Syntax of a while loop:

 

while condition
do

command

done

 

Basically, while the condition is true, do the following command.

 

 

Another example, this time using a colon : to indicate an infinite loop. TRUE could also have been used instead of the :