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:

ADVERTISEMENTS

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
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig 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 VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
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

    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.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.