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 read line; do echo "$line"; done < /etc/passwd
while true; do echo "Running"; done
while [ -f /tmp/stop ]; do echo "Running"; sleep 1; done
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.
What does the `true` command do in a POSIX-compliant shell?
Open an interactive chat with Bash
Why does the `while [ -f /tmp/stop ];` loop terminate while `while true;` does not?
Open an interactive chat with Bash
What happens in `while read line; do echo "$line"; done < /etc/passwd` and why does it terminate?