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 ]

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 (( ; ; ))
   echo "Pres CTRL+C to stop..."
   sleep 1

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 2 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
2 comments… add one
  • tian May 16, 2015 @ 16:44

    thank you..

  • sonic Jan 26, 2016 @ 21:48

    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


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

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum