Bash Loops | Explained

Loops are an essential part of any programming language as they will automate repetitive tasks to save time and minimize the lines of code. Like other languages, Bash scripting supports the looping effect through For Loop, While Loop, and Until Loop.

This guide will teach the audience about BASH Loops through simple examples. The content offered in this post is listed below:

We’ll now explain each of the above in detail across different sections.

What are Bash Loops?

Bash is a scripting language where users can elevate their skills in solving complex problems using Loops. For example, you’re willing to change the names of several files, which could consume lots of time, so you should use the for Loop to automate it. Head on to the next section to learn more about the BASH Loops.

How to Use For Loop in Bash Scripting?

The FOR is a user-controlled Loop that works for a finite or infinite number of iterations. It is used in almost all programming languages because of its simple syntax (which may differ in different languages) like this.

for var in <list>;
do
<Some commands>
done

The FOR Loop always works in ascending order. While var is the variable in which the current item from the <list> in each iteration is stored, the commands between the “do and done” block are executed.

You’ll understand it easily from the example below:

$ for name in john cena savannah summer; do echo $name;done

What we’ve done in the above example is:

  • Created a FOR Loop and initiated “name” as a variable
  • Put four entities in a list (names of people)
  • Between do and done, the echo command is run (to display the contents of the list)

Now, we’ll put the same code in a bash script named For.sh.

#!/bin/bash
for name in john cena savannah summer;
do
  echo $name;
done

The content of the bash script can be seen using.

$ cat For.sh

To get the output, execute the script as.

$ bash For.sh

The output is the same as we used the for loop in the terminal, however; it is obtained through the bash script.

How to Use while Loop in Bash Scripting?

The while loop is used to print/ perform commands until a condition remains true, which is why it is called a condition-controlled loop, as explained in the below example.

Example

For example, we’ve set a condition where the count is LESS THAN OR EQUAL (-le) TO 15; it’ll execute the command until it is true, as seen below.

Code:

#!/bin/bash
count=1
while [ $count -le 15 ]
do
echo $count
((count++))
done
echo nothing more to do

Here, we’ve created a BASH executable file (explained in the first section), then executed it, and the program’s flow is as follows.

  • Initiated the variable “count” with a “1” assigned as its value.
  • In WHILE Loop, the conditional is “less than or equal to,” and it will work before exiting until the condition remains true, i.e., the value of “count” is not greater than fifteen (15).
  • Between the “do” and “done” blocks, we’ve put a command to display the value of count, and after displaying, the increment is done by 1 so that the loop remains functional. If you don’t add a condition, it’ll run forever until your memory runs out or you press “control + z).

The content is obtained using the cat command as:

$ cat While.sh

Execute the script as:

$ bash While.sh

The list of numbers starting from 1 is printed on the terminal.

How to Use Until Loop in Bash Scripting?

The UNTIL Loop is somewhat similar to the WHILE Loop in terms of syntax. The only difference is that it executes the commands until the condition remains false and exits if it is true; here’s the syntax.

Code:

#!/bin/bash
count=10
until [ $count -le 1 ]
do
echo $count
((count--))
done
echo nothing more to do

Now, to help our audience to understand it, we’d break it down.

  • First, we’ve initiated the count variable with the value 10
  • Started the loop block with the condition of COUNT LESS THAN OR EQUAL TO 1 (-le 1).
  • Between the do and done blocks, we’ve added a command (print the value of count) and decrement the value of count by 1 (count–).
  • Type “done” in the script to end the loop and wait for the next increment if the condition is false.

The content of the script is obtained using the cat command.

$ cat Until.sh

Execute the script with the bash keyword.

$ bash Until.sh

The output of the script is printed on the terminal

Conclusion

In Linux, the Bash Loops are helpful in automation tasks, mainly used through a script or Bash executable file. Bash scripting supports all the most used apps, i.e., For, While, and Until. This post has briefly demonstrated the working and usage of all these loops in Bash.