🪟 Windows TippsModify Windows Support Phone Number with PowerShell(03.09.2026 um 00:00 Uhr)
🔧 AI Nachrichten Podcast: ChatGPT schwatzt Nutzern in Deutschland jetzt Werbung auf(28.08.2026 um 08:46 Uhr)
🪟 Windows TippsMicrosoft bringt Emoji 17.0 auf Windows 11(31.08.2026 um 08:16 Uhr)
🪟 Windows TippsModify Windows Support Phone Number with PowerShell(03.09.2026 um 00:00 Uhr)
🔧 AI Nachrichten Podcast: ChatGPT schwatzt Nutzern in Deutschland jetzt Werbung auf(28.08.2026 um 08:46 Uhr)
🪟 Windows TippsMicrosoft bringt Emoji 17.0 auf Windows 11(31.08.2026 um 08:16 Uhr)

📰 IT Security Nachrichten 🕛 vor 1 Jahr 4 Min Lesezeit SECURITY-FEED
0

How to loop forever in bash

↗ Quelle (networkworld.com)
🗣️ Stimme:
📑 Inhaltsübersicht








There are several ways to loop “forever” on Linux commands. In practice, this means only until you decide to stop looping. You might do this while typing commands on the command line, or, more likely, within your scripts. It’s quite easy. This post provides some examples of how to do this.





The for and while commands will provide what you need. There are only a few things you have to keep in mind with respect to syntax and techniques.





Using while





One of the easiest forever-loops involves using the while command followed by the condition “true”. You don’t have to bother with logic like while [ 1 -eq 1 ] or similar tests. The while true test means the loop will run until you stop it with CTRL-C, close the terminal window, log out, or restart the system. After all, true is always true. Here’s an example:





$ while true
> do
> echo Keep running
> sleep 3
> done
Keep running
Keep running
Keep running
^C




The command in the example above will continue running, echo “Keep running” after every three-second break. Try “while false” and it won’t loop even once!





$ while false
> do
> echo Keep running
> sleep 3
> done
$




You can do the same thing with a “while :” test if you prefer. The key here is that the colon always yields success. Like “while true”, this test will never fail and the loop just keeps running. The syntax is just a bit less obvious to anyone who isn’t used to using it.





while :
do
echo Keep running
sleep 3
done




If you’ve inserted an infinite loop into a script and want to remind the person who is using it how to exit the script, you can always provide a little guidance as in the example below.





while :
do
echo Keep running
echo "Press CTRL+C to exit"
sleep 3
done




You can do the same thing in a single line by separating the lines shown above with semicolons as shown in the example below.





while true; do echo Keep running; echo 'Press CTRL+C'; sleep 3; done




The command below would work the same. I prefer while true because it seems obvious, but any of the examples shown will work as does this one:





while [ 1 ]
do
echo Keep running
echo "Press CTRL+C to exit"
sleep 3
done




Using for





The for command also provides an easy way to loop forever. While not quite as obvious as the while true option, the syntax is reasonably straightforward. You can just replace the parameters in a normal for loop (the start, condition and increment values) that would generally look something like this:





$ for (int i = 0; i < 5; i++)




Instead, use the for command with no such values as in the example below.





$ for (( ; ; ))
> do
> echo Keep your spirits up
> echo “Press CTRL+C to exit”
> sleep 3
> done
Keep your spirits up
“Press CTRL+C to exit“
Keep your spirits up
“Press CTRL+C to exit“
Keep your spirits up
“Press CTRL+C to exit“




And, obviously, press CTRL-C when you want to exit the loop.





Why loop forever?





Of course, you’re not ever going to want to loop forever, but having loops run until you stop them can often be very useful. The script below runs until 5:00 p.m. It uses the date command with the +%H option to check the time, then reminds you that it’s time to go home and exits. The sleep 60 command was included to limit the time checking to once a minute. Otherwise, this script could gobble up a lot of CPU time.





#!/bin/bash

while true
do
if [ `date +%H` -ge 17 ]; then
echo Time to go home
exit # exit script
else
sleep 60
fi
done




If you want to exit the loop instead of exiting the script, you can use a break command instead of an exit command as shown in the example below.





#!/bin/bash

while true
do
if [ `date +%H` -ge 17 ]; then
break # exit loop
fi
echo Time to go home
done




Wrap-up





Looping forever is easy. Determining what commands you want to run repeatedly and the conditions under which you want them to stop running are the only challenging parts of putting these forever loops into use.


Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf networkworld.com.
↗ Original-Artikel auf networkworld.com lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:
Community Threat-Level Barometer
Live Votum

Wie stufst du das Risiko dieser Schwachstelle / Bedrohung für dein Unternehmen ein?

Noch keine Stimmen — schätze das Risiko als Erster ein.

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
Modify Windows Support Phone Number with PowerShell
1 Quelle
Die Zukunft des Einkaufens: Warum wir ein neues Kapitel aufschlagen (und wie du es mitschreiben kannst)
1 Quelle
ZDE Podcast 251: Wie sieht digitales Instore Marketing 2026 aus, Amit Chatterjee?
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten How to loop forever in bash

Thematisch verwandte Begriffe: loop, forever, bash · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...