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
Share.
Leave A Reply


Exit mobile version