Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

Bash while Loop - DesignLinux

Oct 31 2020
designlinux 0 Comments
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.

Related

Tags: bash, loop, terminal

How to Install LibreOffice 7.0 on Ubuntu 20.04

Prev Post

Best Practices for Deploying Hadoop Server on CentOS/RHEL 7 – Part 1

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 0
  • 277
  • 1,055,049

DesignLinux.com © All rights reserved

Go to mobile version