Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»Bash Shell»Working with WHILE Loop in Bash Shell Scripting

    Working with WHILE Loop in Bash Shell Scripting

    RahulBy RahulJanuary 8, 20151 Min Read

    Similar to for loop, while loop is also entry restricted loop. It means condition is checked before executing while loop. Mostly it also can do all the works which for loop can do but in uses it has own benefit of use in programming.

    Syntax:

    while [ condition ]
    do
    // programme to execute
    done
  • Read: For Loop Examples in Bash Scripting
  • While Loop Example in Bash

    For example following loop will be executed 10 times and exited when value of i will be greater than 10.

    #!/bin/bash
    
    i=1
    while [ $i -le 10 ]
    do
       echo "This is looping number $i"
       let i++
    done
    

    Infinite While Loop in Bash

    Infinite for loops can be also known as never ending loop. The following loop will execute continuously until stopped forcefully using CTRL+C.

    #!/bin/bash
    
    while true
    do
      echo "Press CTRL+C to Exit"
    done
    

    But we can conditional statement like if to terminate loop on matching any specific condition. Read more about working with if-else in bash scripting.

    #!/bin/bash
    
    while true
    do
       if [ condition ];do
          exit
       fi
    done
    

    C-Style while Loop

    In bash script we can also write while loops pretty similar to c programming language.

    #!/bin/bash
    
    i=1
    while((i <= 10))
    do
       echo $i
       let i++
    done
    

    Reading File Content using While Loop

    While also provide option’s to read file content line by line, which is an very useful uses of while loop while working with files.

    #!/bin/bash
    
    while read i
    do
       echo $i
    done < /tmp/filename.txt
    

    In this while loop read one line from file in one loop iteration and store value in variable i.

    bash loop script shell shell scripts while
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleWorking with FOR Loop in Bash Shell with Examples
    Next Article How to Install PostgreSQL 9.6 on CentOS/RHEL 7/6 and Fedora 26/25/24

    Related Posts

    How to Backup Website to Amazon S3 using Shell Script

    Updated:March 24, 20222 Mins Read

    Bash Printf Command

    Updated:December 23, 20212 Mins Read

    Bash Sequence Expression (Define Range)

    Updated:January 12, 20222 Mins Read

    Bash Select Command (Create Menu in Shell Scripts)

    Updated:December 10, 20212 Mins Read

    (Resolved) -bash: /bin/mv: Argument list too long

    Updated:January 13, 20222 Mins Read

    Bash Break and Continue

    Updated:November 9, 20213 Mins Read

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Install Sublime Text 4 on Ubuntu 22.04
    • How to Enable / disable Firewall in Windows
    • How to Install JAVA on Ubuntu 22.04
    • Switching Display Manager in Ubuntu – GDM, LightDM & SDDM
    • Changing the Login Screen Background in Ubuntu 22.04 & 20.04
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.