• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

Bash while Loop

Written by Admin, Updated On November 8, 2020
bash, loop, terminal
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.

It 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.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Install LibreOffice 7.0 on Ubuntu 20.04
Next Article   How to Create Bash Aliases

Related Posts

  • How to Install SSH Keys on Ubuntu 22.04

    How to Set up SSH Keys on Ubuntu 22.04

    January 7, 2023
  • How to Install Fail2ban on Ubuntu 22.04

    How to Install and Configure Fail2ban on Ubuntu 22.04

    December 5, 2022
  • How to Enable SSH on Ubuntu 22.04

    How to Enable SSH on Ubuntu 22.04

    December 1, 2022

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install SSH Keys on Ubuntu 22.04
    How to Set up SSH Keys on Ubuntu 22.04 January 7, 2023
  • How to Install Mongodb on Debian 11
    How to Install MongoDB on Debian 11 Linux January 11, 2023
  • How to Install Puppet Agent on Ubuntu 22.04
    How to Install Puppet Agent on Ubuntu 22.04 January 22, 2023
  • How to Install Jenkins on Debian 11
    How to Install Jenkins on Debian 11 January 5, 2023
  • How to Change-Hostname Ubuntu 22.04
    How to Change Hostname on Ubuntu 22.04 January 19, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook