BASH While Loop Examples

28/12/2020
Three types of loops are used in bash programming. While loop is one of them. Like other loops, while loop is used to do repetitive tasks. How you can use while loop in bash script is shown in this article by using different examples.

Syntax of while loop:

while [ condition ]
do
    commands
done

The starting and ending block of while loop are defined by do and done keywords in bash script. Termination condition is defined at the starting of the loop. Open a text editor to write bash script and test the following while loop examples.

Example-1: Iterate the loop for fixed number of times

Create a bash file named while1.sh which contains the following script.

n=1
while [ $n -le 5 ]
do
       echo "Running $n time"
       (( n++ ))
done

In this example, the loop will iterate for 5 times and print the text which is defined inside the loop. The following output will appear if you run while1.sh.

Example-2: Using break statement for conditional exit

break statement is used to exit from the loop early based on a particular condition. Create a new bash file named while2.sh with the following code.

n=1
while [ $n -le 10 ]
do
    if [ $n == 6 ]
    then
           echo "terminated"
           break
     fi
     echo "Position: $n"
     (( n++ ))
done

In this example, the loop is declared to iterate for 10 times.  According to the script it will terminate after 6 times iteration for break statement. The following output will appear after executing the script.

Example-3: Using continue statement to omit particular step

Create a new bash file named while3.sh with the following code.

n=0
while [ $n -le 5 ]
do
     (( n++ ))
 
     if [ $n == 3 ]
     then
           continue
     fi
     echo "Position: $n"
 
done

In this example, the loop will iterate for 5 times but it will not print all 5 positions. When the loop will iterate for 3rd times then continue statement will be executed and the loop will go for the next iteration without printing the text of 3rd position. The following output will appear after executing the script.

Example-4: Creating infinite loop

 Sometimes, it is required to declare infinite loop for various programming purposes. Create a new bash file named while4.sh and test the code of infinite loop.

n=1
while :
do
         printf "The current value of n=$nn"
         if [ $n == 3 ]
         then
                   echo "good"
         elif [ $n == 5 ]
         then
                  echo "bad"
         elif [ $n == 7 ]
         then
                  echo "ugly"
         elif [ $n == 10 ]
         then
                   exit 0
         fi
         ((n++))
done

No termination condition is set for the loop in this example. This type of loop is called infinite loop. Here, exit statement is used to quit from the infinite loop. So, this loop will iterated for 10 times and when the iteration value become equal to 10 then exit statement will execute for exiting from the infinite loop.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Using and Customizing Bash Command History

The bash shell is the default command line environment available in most Linux distributions. Similar to all shell environments,...
29/12/2020

How to Search for your Files on the Linux Command Line

For a Linux desktop, a user can easily install an app to search their files and folders in the file system, but another...
29/12/2020

Bash Arithmetic Operations

Doing arithmetic operations in bash is not similar to other standard programming languages. One of the limitations of bash...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024

Thuê mua IPv4 giá rẻ, tốc độ nhanh, uy tín #1
28/05/2024

Thuê địa chỉ IPv4 IPv6 trọn gói ở đâu chất lượng, giá RẺ nhất?
27/05/2024