A junior administrator is experimenting with arithmetic loops in a Bash script. Examine the fragment below:
#!/usr/bin/env bash
num=2
until (( num++ >= 5 )); do
echo -n "$num "
done
echo # prints a final newline
The script is run under Bash 5.x with default settings and exits normally. What exact sequence is written to standard output before the shell prompt returns?
The loop never terminates because the condition is written incorrectly
An until loop keeps executing its body while the test command returns a non-zero exit status and stops as soon as the test succeeds (exit status 0). The test here is the arithmetic expression num++ >= 5, which is evaluated before each iteration. Because the postfix increment operator returns the variable's current value and then increments it, the first comparison checks 2 >= 5, which is false (result 0 → exit status 1). The loop therefore enters its body and prints the now-incremented value 3. The next two tests evaluate 3 >= 5 and 4 >= 5, both still false, so the body executes and prints 4 and then 5. When num reaches 5, the following test compares 5 >= 5; the expression evaluates to 1, giving an exit status 0, so the loop terminates and the body is not executed again. Consequently, only the numbers 3 4 5 are produced, followed by the newline from the final echo. The other answer choices either include the pre-increment value 2, continue one step beyond the terminating condition, or ignore that the loop stops once the test succeeds.
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 is the difference between 'until' and 'while' loops in Bash?
Open an interactive chat with Bash
How does the postfix increment operator 'num++' work in Bash arithmetic?
Open an interactive chat with Bash
Why does the script output start with 3 instead of 2?
Open an interactive chat with Bash
CompTIA Linux+ XK0-006 (V8)
Automation, Orchestration, and Scripting
Your Score:
Report Issue
Bash, the Crucial Exams Chat Bot
AI Bot
Loading...
Loading...
Loading...
IT & Cybersecurity Package Join Premium for Full Access