Articles

Home / Articles / DevOps / How to add loop in Bash Script?

How to add loop in Bash Script?

Published on Oct. 27, 2021

In order to run a piece of commands again and again and again, we use loops. Loops help us to avoid the repetitive code writing procedure and do multiple things in minimal lines of code.

 

In this article on bash scripting, we are going to discuss how we can actually implement the loops in our script and reduce the lines of code.

 

Table of content

 

What is Loop?

Loop is similar to what it sounds, "loop",  "something that occurs again and again and again".  In programming, when we are required to do a certain task multiple times rather than writing that piece of code multiple times we use a loop.

 

Loops allow us to take a series of commands and keep re-running them until a particular situation is reached. They are useful for automating repetitive tasks.

 

In Bash scripting, there are 3 different types of loops:

 

While Loop

While is one of the easiest ways of looping. In this loop, the compiler checks the condition if it is true it runs the body of the loop and whenever the condition becomes false it comes out of the loop and executes the further code.

 

Syntax 

while [condition]
do
statements
done

 

Let's understand this with an example. Suppose we want to print a count of 1 to 10. Here is the script for that -

 

#!/usr/bin/bash
count=1
while [ $count -le 10]
do
  echo $count
  (( count++ ))
done

 

Let's understand the code one by one:

  1.  It is the path of the interpreter
  2. A counter variable that is initialized with value 1
  3. while loop with a condition that count is less than or equal to 10.
  4. do - to perform the task
  5. print the value of count
  6. to increment the count with one
  7. done - to end the loop

 

 

Until Loops

Until is similar to while loop. it is just the negation of the while loop. It runs the loop until the test becomes true. 

 

until [ condition ]
do
command
done

 

We can run the same example using the until just changing the condition

 

#!/usr/bin/bash
count=1
until [ $count -gt 10]
do
  echo $count
  (( count++ ))
done

 

For Loop

The for loop is a little bit different from the previous two loops.  This loop seems a bit tricky but once you get handy with this, this will be extremely helpful for you.

 

for var in <list>
do
statements
done

 

Let's try to print elements of a list

#!/usr/bin/bash
list="Hello how are you"
for var in $list
do
echo $var
done

 

We can also do a similar example of printing the count from 1 to 10 in this.

 

#!/usr/bin/bash
for var in range{1..10}
do
echo $var
done

 

Break 

The break statement is useful when we want to come out of the loop after a certain condition is met. Sometimes we don't know the exact time our need will fulfill then we use the break statement. Condition check every time the loop runs and when the condition meets, the break statement comes to the action and our program comes out of the loop.

 

For example - 

 

#!/usr/bin/bash
for var in range{1..10}
do
  echo $var
  if [ $var == 5 ]
  then
    break
  fi
done

 

In the above example, the loop will stop running once the value of the var variable becomes 5.

 

In case you don't have knowledge about the conditional statement you can visit this article - How to add conditions in your Bash Script?

 

Continue

The continue statement in Bash script helps the script to skip the loop if a certain condition is reached. There might be some cases when we don't want to run our loop then using the continue we can actually skip the loop.

 

#!/usr/bin/bash
for var in range{1..10}
do
  if [ $var == 5 ]
  then
    continue
  echo $var
done

 

Final Voice

In this article, we have discussed the different types of loops in a Bash script with break and continue statements.

 

You can connect with the author on Twitter.

···