Bash Infinite Loop Examples

See all Bash/Shell scripting related FAQ
How do I write an infinite loop in Bash script under Linux or UNIX like operating systems?

An infinite loop is nothing but a sequence of instructions which loops endlessly, either due to the loop having no terminating condition, having one that can never be met, or one that causes the loop to start over. The syntax is as follows using the while loop.
Tutorial details
Difficulty level Easy
Root privileges No
Requirements Linux or Unix terminal
Category Linux shell scripting
OS compatibility BSD Linux macOS Unix
Est. reading time 2 minutes

Bash Infinite Loop Syntax

The syntax is:

#!/bin/bash
while :
do
	echo "Press [CTRL+C] to stop.."
	sleep 1
done

This is a loop that will forever print “Press [CTRL+C] to stop..”. Please note that : is the null command. The null command does nothing and its exit status is always set to true. You can modify the above as follows to improve the readability:

#!/bin/bash
while true
do
	echo "Press [CTRL+C] to stop.."
	sleep 1
done

Bash Infinite Loop Examples At The CLI

A single-line bash infinite while loop syntax is as follows:

 while :; do echo 'Hit CTRL+C'; sleep 1; done

OR

 while true; do echo 'Hit CTRL+C'; sleep 1; done

Bash for infinite loop example

Here is another example:

#!/bin/bash
 
for (( ; ; ))
do
   echo "Pres CTRL+C to stop..."
   sleep 1
done

How Do I Escape The While Loop?

A for or while loop may be escaped with a break statement when certain condition is satisfied:

 ### for loop example ###
for (( ; ; ))
do
   echo "Pres CTRL+C to stop..."
   sleep 1
   if (disaster-condition)
   then
	break       	   #Abandon the loop.
   fi
done

OR

 ### while loop example ###
while :
do
   echo "Pres CTRL+C to stop..."
   sleep 1
   if (disaster-condition)
   then
	break       	   #Abandon the loop.
   fi
done

Using case statement to exit infinite bash while loop

You can also use the case statement to escape with a break statement:

while :
do 
     ### add some input and output here ###
     case $var in
       yes) do something ;;
       no) do something ;;
       quit) break ;;        ##Abandon the loop.
     ease
done

Example

A sample shell script to demonstrate the actual usage of an infinite loop and the break statement:

#!/bin/bash
# Purpose: Display various options to operator using menus
# Author: Vivek Gite < vivek @ nixcraft . com > under GPL v2.0+
# ---------------------------------------------------------------------------
# capture CTRL+C, CTRL+Z and quit singles using the trap
trap '' SIGINT
trap ''  SIGQUIT
trap '' SIGTSTP
 
# display message and pause 
pause(){
	local m="$@"
	echo "$m"
	read -p "Press [Enter] key to continue..." key
}
 
# set an 
while :
do
	# show menu
	clear
	echo "---------------------------------"
	echo "	     M A I N - M E N U"
	echo "---------------------------------"
	echo "1. Show current date/time"
	echo "2. Show what users are doing"
	echo "3. Show top memory & cpu eating process"
	echo "4. Show network stats"
	echo "5. Exit"
	echo "---------------------------------"
	read -r -p "Enter your choice [1-5] : " c
	# take action
	case $c in
		1) pause "$(date)";;
		2) w| less;;
		3) echo '*** Top 10 Memory eating process:'; ps -auxf | sort -nr -k 4 | head -10; 
		   echo; echo '*** Top 10 CPU eating process:';ps -auxf | sort -nr -k 3 | head -10; 
		   echo;  pause;;
		4) netstat -s | less;;
		5) break;;
		*) Pause "Select between 1 to 5 only"
	esac
done

Summing up

You learned how to use bash infinite loop at the CLI or in your Linux or Unix script with various examples.

See also:

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

15 comments… add one
  • Cpt. Varel May 16, 2017 @ 8:35

    The case “*)” in the “Example” above is not working:
    *) Pause “Select between 1 to 5 only”

    The message is not displayed if a number >5 is entered.

    I’ve changed it to

    *) echo “Select between 1 to 5 only”; pause

    Then it’s working. Only for the sake of form :)

    • Cpt. Varel May 16, 2017 @ 8:50

      Forget my 1st comment.
      Just saw that “Pause” was written up.
      If it is small-written, it works naturally ;)

  • d.r. Aug 10, 2020 @ 12:23

    This joke can break your filesystem:

    #!/bin/bash
    while :
    do
    	mkdir a
    	cd a
    done
    

Leave a Reply

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

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.