Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Programming»Bash Shell»How to Create Infinite while Loop in Bash Script

    How to Create Infinite while Loop in Bash Script

    By RahulNovember 27, 20141 Min Read

    Command:



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

    Advertisement

    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

    Related Posts

    10 Bash Tricks Every Developer Should Know

    Understanding 2>&1 in Bash: A Beginner’s Guide

    Best Practices and Tips for Writing Shell Scripts as Pro

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • How to List Manually Installed Packages in Ubuntu & Debian
    • 10 Bash Tricks Every Developer Should Know
    • How to Validate Email Address in JavaScript
    • Firewalld: Common Firewall Rules and Commands
    • 12 Apk Commands in Alpine Linux Package Management
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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