≡ Menu

Linux / UNIX: Bash Loop Forever

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

Sysadmin because even developers need heroes!!!

Share this tutorial on:
{ 2 comments… add one }
  • tian May 16, 2015, 4:44 pm

    thank you..

  • sonic January 26, 2016, 9:48 pm

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

    doesn’t quite work, but:

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

    does.

    why not just do cat /boot/vmlinuz. There’s nothing like a Kernel image to the face to make you learn more about Linux.

Security: Are you a robot or human?

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">


   Tagged with: , , , , , ,