Linux / UNIX: Bash Loop Forever

by on January 1, 2011 · 0 comments· last updated at May 19, 2011

I need to cycles through a loop forever using for or while syntax. How do I set such loop under UNIX or Linux operating systems?

The syntax is as follows to cycle through a loop forever using while loop:

while [ 1 ]
do
    command1
    command2
done
 

OR use the following syntax:

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

For loop example

To cycle through a loop forever using for loop:

 
for (( ; ; ))
do
   echo "Pres CTRL+C to stop..."
   sleep 1
done
 


You should follow me on twitter here or grab rss feed to keep track of new changes.

Featured Articles:

{ 0 comments… add one now }

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <kbd> <blockquote> <pre> <a href="" title="">

Tagged as: , , , , , ,

Previous Faq:

Next Faq: