Using the for loop

The for loop is used for doing certain actions repetitively. The syntax is as follows:

for(initialization; condition; increment/decrement) 
actions 

Initially, a variable is initialized then the condition is checked. If it is true, then the action or actions enclosed in curly brackets are performed. Then, the variable is incremented or decremented. Again, the condition is checked. If the condition is true, then actions are performed; otherwise, the loop is terminated.

An example of the awk command with the for loop is as follows:

    $ awk '{ for( i = 1; i <= NF; i++) print NF,$i }' people.txt
  

Initially, the i variable is initialized to 1. Then, the condition is checked to see whether i is less than NF. If true, then the action ...

Get Learning Linux Shell Scripting - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.