Which of the following while-loop constructs will create an infinite loop in a POSIX-compliant shell script (assuming no external signals are sent and no files are modified during execution)?
while false; do echo "Running"; done
while true; do echo "Running"; done
while [ -f /tmp/stop ]; do echo "Running"; sleep 1; done
while read line; do echo "$line"; done < /etc/passwd
The loop in option 1 uses the command true, which always exits with status 0 (success). Because the while condition evaluates to success on every iteration, the body executes indefinitely, resulting in an infinite loop.
Option 2 will terminate if the file /tmp/stop is removed, option 3 ends when it reaches end-of-file on /etc/passwd, and option 4 never runs because the condition false immediately returns a non-zero status.
Ask Bash
Bash is our AI bot, trained to help you pass your exam. AI Generated Content may display inaccurate information, always double-check anything important.
How does the 'while' construct work in shell scripting?
Open an interactive chat with Bash
What are some potential uses for an infinite loop in a shell script?
Open an interactive chat with Bash
What does the 'true' command do in a shell script?