Emulating a do-while loop in Bash

In the Bash script, the loop such as “for” and “while” is extensively used for repeating/automating the tasks. The do-while is the loop examined in the programming languages like C++ and OOP that executes at least once and then further executes if the condition is true. The do-while support is not available in the bash script. However, the user can emulate the “while” loop with “do” for getting the features of the do-while loop.

This guide will demonstrate to emulate the do-while loop in the bash script with the following outline.

  • Emulate the do-while Loop in Bash
  • Generate a Number of Lists 
  • Break Statement in the loop
  • Continue Statement in the loop

How to Emulate the do-while Loop in Bash?

The syntax to emulate the do-while loop in the bash script is described below:

Syntax:

while [condition]
do
  Command 1
  Command 2
  Command 3
  .
  .
  .
  Command n
  Inreemnt/decrement operators
done

The syntax is described as: 

  • The “while” loop with the [Condition] to be applied.
  • The “do” section executes multiple commands when the condition is true and increment/decrement operators to continue the loop.

Example 1: Generate a Number of Lists 

To generate the number of lists from 1 to 5 using the above syntax is obtained as follows:

#!/bin/bash
i=1
while [ $i -le 5 ]
do
    echo "Number $i"
    (( i++ ))
done

The script is defined as: 

  • The “i” variable is initialized with 1.
  • The “while” loop with the condition “[ $i -le 5 ]” to check the “i” variable is less than and equals to 5.
  • The “do” section executes that prints the “$i” variable through the “echo” command and increments in the variable using the expression “((i++))”.

Save the above script in the file and exit.

Run the script using the bash command in the terminal:

$ bash script.sh

The number of the list from 1 to 5 is printed on the screen.

Example 2: Break Statement in the loop

To skip any number of iterations from printing, utilize the below script:

#!/bin/bash
read -p "Enter Number Between 1 to 10 to Break at Specific Point:" num
i=1
while [ $i -lt 10 ]
do
  if [[ "$i" == "$num" ]]; then
    break
  fi
  echo "The Number is: $i"
((i++))
done

The script is defined as:

  • The “read” property takes input from the user to apply break at the specific point between 1 to 10 and stored in the “num” variable.
  • The “i” variable is initialized with 1.
  • The “while” loop with the condition “[ $i -lt 10 ]” to check the “i” variable is less than 10.
  • Then in the “do” section, the “if” condition “[[ “$i” == “$num” ]]” to check if the entered number is matched to apply break.
  • The “echo” statement for printing the “$i” variable.
  • The increment in the “i” variable using the expression “((i++))” to continue the loop.

Save the above script and exit from the editor.

Run the script to test the results:

$ bash script.sh

The entered number is 4 to apply break so only 3 iterations are carried out. 

Example 3: Continue Statement in the loop

Likewise, the user can also use the “continue” statement in a loop to skip a particular iteration. The following script takes the input from the user to exclude a particular iteration using the “continue” statement:

#!/bin/bash
read -p "Enter Number Between 1 to 10 to Exclude Any Point:" num
i=0
while [ $i -lt 10 ]
do
  ((i++))
  if [[ "$i" == "$num" ]]; then
    continue
  fi
  echo "The Number is: $i"
done

The script is defined as:

  • The “read” property takes input from the user to apply “continue” to skip particular iteration between 1 to 10.
  • The “i” variable is initialized with 0.
  • The “while” loop with the condition “[ $i -lt 10 ]” to check the “i” variable is less than 10.
  • Then in the “do” section, increments in the “i” variable using the expression ((i++)) and the “if” condition “[[ “$i” == “$num” ]]” to check if the entered number is matched to apply the “continue” statement.
  • The “echo” statement for printing the “$i” variable.

Save the script in the file and run it in the terminal using the bash command:

$ bash script.sh

The number of lists from 1 to 10 is printed and the 4th iteration is skipped because the user enters 4 to exclude the 4th iteration.

Note: To know more about the do-while loop in bash, explore our latest guide here. 

Conclusion

Because the Bash doesn’t support the do-while loop properly but can be emulated using the while loop with “do” to obtain the same results. The user can generate number lists and apply the break statements to stop the execution or apply to continue statements to exclude any iteration. In short, the users can do all that a “do while” loop can do. 

This write-up has enlightened the way to emulate the do-while loop in the bash script.