Site icon DesignLinux

Bash while Loop

Bash while Loop

Loops are primary requirement of any programming languages. Loops are useful when you want to execute a series of commands until the certain condition is satisfied. In Bash, loops are useful for automating repetitive tasks. In this tutorial, we will see basics of while loop in Bash.

There are three basic loops for loop, while loop , and until loop. We will also learn how to use the break and continue statements.

Bash while Loop

The while loop is used to perform the given set of commands for n number of times until the given condition is not met.

Below is the primary form of while loop in Bash:

while [CONDITION]
do
  [COMMANDS]
done

In that, the while statement starts with the while keyword and followed by the conditional expression.

If will first check the condition before executing the commands. If the condition is true then commands will executed. Otherwise, the loop terminates and program control goes to next command.

Let’s see an example, to print the number which increment by one, until the condition is not true.

i=0

while [ $i -le 5 ]
do
  echo Number: $i
  ((i++))
done

Loop will iterate until the value of i is less or equal to five. It will print the following output:

Number: 0
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5

Infinite while Loop

The loop which repeats indefinitely and never terminates is infinite loop. Means until the condition evaluates to true, it will infinite loop.

In the following example, we are using the built-in command : to create an infinite loop. : always returns true. You can also use the true built-in or any other statement that always returns true.

while :
do
  echo "Press <CTRL+C> to exit."
  sleep 1
done

The above example of while loop will run indefinitely and will never terminate. You can terminate the loop by pressing CTRL+C.

You also can write in single-line like this:

while :; do echo 'Press <CTRL+C> to exit.'; sleep 1; done

Read a File Line By Line

Commonly, the while loop is use to read a file, variable line by line or data stream. Below is given the example to read the /etc/passwd file line by line and prints each line:

file=/etc/passwd

while read -r line; do
  echo $line
done < "$file"

Here, we are controlling the loop using the input redirection (< "$file") to pass a file to the read command instead of controlling with a condition. The loop will run until the reading of last line.

It’s recommended to use the -r option with the read to prevent backslash from acting as an escape character.

The read command removes the leading and trailing whitespace by default. To prevent from such behavior you can use the IFS= option before read:

file=/etc/passwd

while IFS= read -r line; do
  echo $line
done < "$file"

break and continue Statements

You can control the for loop execution by using the break and continue statements.

break Statement

The break statement is usually used to terminate the loop when a certain condition is met. It terminates the current loop and passes program control to the statement that follows the terminated statement.

In the below example, the execution of the loop will be terminate once the current iterated item is equal to 4.

i=0

while [ $i -lt 10 ]
do
  echo "Number: $i"
  ((i++))
  if [[ "$i" == '4' ]]; then
    break
  fi
done

echo 'Done!'
Number: 0
Number: 1
Number: 2
Number: 3
Done!

continue Statement

The continue statement is used to exit from the current iteration of a loop and go to the next iteration.

In the following example, when the current iterated item is equal to 4 the continue statement will divert control to the beginning of the loop and to continue with the next iteration.

i=0

while [ $i -lt 10 ]
do
  ((i++))
  if [[ "$i" == '4' ]]; then
    continue
  fi
  echo "Number: $i"
done

echo 'Done!'
Number: 1
Number: 2
Number: 3
Number: 5
Number: 6
Number: 7
Number: 8
Number: 9
Number: 10
Done!

Conclusion

In this guide, you learned how to use the while loop in bash script. The while loop is useful when you wants to repeatedly execute a set of commands until the condition is true.

If you have any questions or feedback, feel free to leave a comment.

Exit mobile version