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

Bash tr command

tr is a very useful UNIX command. It is used to transform string or delete characters from the string. Various type of...
29/12/2020

BASH While Loop Examples

Three types of loops are used in bash programming. While loop is one of them. Like other loops, while loop is used to do...
28/12/2020

Bash `pushd` command

The Linux users may need to switch between many directories for doing a particular task and it is a time consuming task...
29/12/2020
Bài Viết

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

Mua proxy v4 chạy socks5 để chơi game an toàn, tốc độ cao ở đâu?
18/05/2024

Thuê mua proxy Telegram trọn gói, tốc độ cao, giá siêu hời
18/05/2024

Thuê mua proxy Viettel ở đâu uy tín, chất lượng và giá tốt? 
14/05/2024

Dịch vụ thuê mua proxy US UK uy tín, chất lượng số #1
13/05/2024

Thuê mua proxy Việt Nam: Báo giá & các thông tin MỚI NHẤT
13/05/2024