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»How to Create Infinite while Loop in Bash Script

    How to Create Infinite while Loop in Bash Script

    RahulBy RahulNovember 27, 20141 Min Read

    Command:



    while true;do echo “Press CTRL+C to Exit”; done


    Example 1:

    Some times we are required to execute some commands or processes continuously until stop it forcefully or conditionally. The following example of while loop will run continuously until stopped forcefully using ctrl + c.

    while true
    do
      echo "Press CTRL+C to Exit"
    done
    

    Example 2:

    We can also use colon “:” in place of “true” with while loop for creating infinite loop in bash script.

    while :
    do
      echo "Press CTRL+C to Exit"
    done
    

    Stopping Loop on Condition:

    In case we need to terminate an infinite while loop on matching certain condition’s, we can use break keyword to exit from running loop.

    while :
    do
      echo "Press CTRL+C to Exit"
      if [ condition ]
      then
          break
      fi
    done
    
    bash infinite loop while
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Count Lines in a file in Linux
    Next Article How to Compare Two Strings using If in Bash Script

    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

    Creating Menu in Shell Script (Linux Select Command)

    Updated:May 31, 20222 Mins Read

    Bash Break and Continue

    Updated:November 9, 20213 Mins Read

    Bash String Concatenate

    Updated:January 5, 20222 Mins Read

    Leave A Reply Cancel Reply

    Recent Posts
    • What is CPU? – Definition, Types and Parts
    • What is the /etc/aliases file
    • What is the /etc/nsswitch.conf file in Linux
    • How to Install Ionic Framework on Ubuntu 22.04
    • What is the /etc/hosts file in Linux
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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