Welcome to the sixth chapter on BASH scripting series. And today we will dive into a very cool topic called looping constructs in computer programming. Loops are important if we have a task that needs repetition. In the previous chapter where we discussed decisions, we made the computer think logically. Combine that with loops and we have an almost humanoid program that can think, work, and repeat the steps throughout the day.
What are loops?
Loops are control flow statements that allow a program to execute the common repetitive task (or statements) up to a particular number of times.Here’s a simple analogy, we wake up and work through 9 AM till 5 PM then sleep for the night and repeat the same process all over again. So we humans too are kind of in a loop… Work time, time’s up, and repeat again.
However, the loop we are about to implement as BASH scripts is controlled so it doesn’t execute endlessly. We will further analyze loops by relating to work time duration 9 AM – 5 PM.
- Every loop control statement will usually have a starting point and an ending point. In our work duration situation, 9 AM is the starting point and 5 PM (or 15:00 in 24-hour format) is the ending point.
- Then there is a counter that increments or decrements the value from the starting point. And once the counter reaches the ending point, the loop stops. Looking back at the work time duration, our counter would increase in small increments (assume hourly) until it reaches 15:00 and then we go home for the night.
Loops in BASH
There are three loop statements in BASH namely: for, while, and until. The former two ie., for and while loops are popular in most of the other higher level languages such as C, C++, Java, etc. Read on below to learn more about them
for is a control flow statement in which a group of statements is executed continuously as long as the condition is true. Below is the syntax for for loop:
for var in list do statements done
Here var is the starting point and list is the ending point. var will be either incremented or decremented by the list of statements specified in list.
Like for loop, while loop executes a group of statement continuously as long as the condition is true. Below is the syntax for while loop:
while condition is true do statements increment/decrement done
The only difference between a while loop and for loop is that in for loop, increment/decrement counterpart is done on the same line where we wrote our for statement. Whereas, in while loop, increment/decrement part is done after its body of statements is executed.
However, you should note that increment/decrement part after the while body statements is not always the case and may vary depending on the type of BASH script one is implementing (for instance an infinite while loop).
until loop is similar to while loop, also executes a group of statement continuously but with an exception in condition statement part where its boolean value is tested for false and not true. So that means, once the condition becomes true the loop exits otherwise the loop continues as long as the condition is false.
until condition is false do statements increment/decrement done
Bash Scripting Examples
#!/bin/bash i=0 for i in 1 2 3 4 5 do echo i is $i done
In the above for loop, variable i is the starting point and 1 2 3 4 5 are the list. At each iteration I value is displayed on the screen then is incremented by 1 and the loop continues until it reaches 5 after which the loop successfully exits.
#!/bin/bash counter=0 while [ $counter -lt 3 ] do echo counter is now $counter # increment counter by 1 counter=$(( $counter + 1 )) done
In the above example script, we are comparing if the counter value is less than 3. Since its starting point is 0 the condition results in true boolean value and the while body is executed. Then the variable counter is incremented by 1 and the loop continues. Soon when the counter value reaches 3, the condition results in false boolean value because 3 cannot be greater than 3, the loop successfully exits.
#!/bin/bash counter=0 until [ $counter -gt 3 ] do echo counter is now $counter counter=$(( $counter + 1 )) done
Here the condition is set to compare if the counter value is greater than 3. At the starting point, the counter value is 0 so the condition results in false boolean value and the body of statements are executed. When the counter value reaches 4, the condition results in true boolean value and the loop is successfully exited.
Loops in BASH simplify repetitive codes by allowing the programmer to type less but do more. In case you wanted to print “Hello World!” 20 times on your console screen, you have two ways to achieve that: either type 20 Hello World by hand or use a loop that increments up to 20 printing the greeting message. I’m sure you’d go with the latter one right?Moreover, apart from the controlled loop we practiced in the above example scripts, you can also create a loop that executes infinitely. That’ll be your homework for this topic. I’ll see you in the next article so stay tuned…