Skip to content

How to Create Infinite while Loop in Bash Script

How to Create Infinite while Loop in Bash Script. Infinite looping in bash shell script using while loop. While infinite loop

On this page

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

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top