How to Create Infinite while Loop in Bash Script

Channel: Linux
Abstract: In case we need to terminate an infinite while loop on matching certain condition’swe can use break keyword to exit from running loop. while
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

Ref From: tecadmin

Related articles