Bash Infinite Loop Shell Linux


How can write infinite shell that will run forever? May be I need a 5 second sleep between steps. Recently I have a issue with my routing table and I need to remove some route automatically every 5 minutes. I came up with a solution that runs every 4 minutes and clears unwanted routes.

While Infinite Loop

There are a lot of different ways do express our solution one of them is when loop of bash. We will provide true to the while.

$ while true; do echo "test"; sleep 5; done
While Infinite Loop
While Infinite Loop

Here while true runs loop forever between do and done is run regularly but sleep 5 makes the script or each step to wait 5 seconds. We can cancel script with CTRL+C.

For Infinite Loop

We can also implement infinite loops with the for . We will provide ( ; ; ) to the for loop which means infinite loop. We will print test to the console forever with 5 seconds intervals.

$ for (( ; ; )) do echo "test"; sleep 5; done
For Infinite Loop
For Infinite Loop

 

Bash Infinite Loop Shell Linux Infografic

LEARN MORE  Cockpit Easy and Web Based Administration of Linux

Leave a Comment