Introduction
Infinite loops keep running a set of commands forever until you stop them. They help when you want a task to repeat without stopping.
while true; do # commands sleep 1 done
true always returns success, so the loop never ends on its own.sleep to pause between commands and avoid using too much CPU.while true; do echo "Hello" sleep 1 done
: is a shortcut for true.while :; do date sleep 2 done
for (( ; ; )); do echo "Looping" sleep 3 done
break when count is greater than 5.#!/bin/bash count=1 while true; do echo "Count is $count" ((count++)) if [ $count -gt 5 ]; then echo "Stopping loop" break fi sleep 1 done
break condition or pressing Ctrl+C.sleep, infinite loops can use a lot of CPU and slow down your system.while true, while :, or for (( ; ; )) to create them.while true runs the loop forever because the condition is always true.while true; do echo Hello; done uses while true correctly. for i in 1..10; do echo $i; done loops 10 times, not infinite. if true; then echo Loop; fi is a conditional, not a loop. while false; do echo No; done never runs because condition is false.for (( ; ; )) creates an infinite loop because it has no start, condition, or increment limits.for (( ; ; )); do echo Loop; done matches infinite loop syntax. for i in {1..5}; do echo $i; done loops 5 times. for i in ; do echo $i; done is invalid syntax. for (( i=0; i<5; i++ )); do echo $i; done loops 5 times with increment.count=1
while :; do
echo $count
((count++))
if [ $count -gt 3 ]; then
break
fi
donewhile : which is an infinite loop, but it has a break condition inside.while true echo "Running" done
do after the while condition to start the loop body.while true but no do before echo. This causes a syntax error.stop.txt exists. Which script correctly implements this?while true; do
if [ -f stop.txt ]; then
break
fi
echo "Waiting..."
sleep 2
done uses while true with a break if stop.txt exists, which stops the loop correctly.stop.txt first and breaks if the file exists, otherwise prints "Waiting..." and sleeps 2 seconds. This matches the requirement.Pass the test and this pattern moves unknown