0
0
Bash Scriptingscripting~10 mins

until loop in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - until loop
Initialize variable
Check condition
EXIT
Update variable
Back to Check condition
The until loop runs the code inside it until the condition becomes true. It checks the condition first, runs the body only if the condition is false, then repeats.
Execution Sample
Bash Scripting
count=1
until [ $count -gt 3 ]
do
  echo $count
  ((count++))
done
This script prints numbers 1 to 3 using an until loop that stops when count is greater than 3.
Execution Table
StepcountCondition [ $count -gt 3 ]Condition ResultActionOutput
111 -gt 3FalsePrint 1, increment count to 21
222 -gt 3FalsePrint 2, increment count to 32
333 -gt 3FalsePrint 3, increment count to 43
444 -gt 3TrueExit loop
💡 count reaches 4, condition 4 -gt 3 is True, so loop stops
Variable Tracker
VariableStartAfter 1After 2After 3Final
count12344
Key Moments - 2 Insights
Why does the loop run when the condition is false, not true?
The until loop runs while the condition is false. It stops when the condition becomes true, as shown in execution_table rows 1-3 where condition is false and loop runs, then row 4 where condition is true and loop exits.
What happens if the condition is true at the start?
If the condition is true initially, the loop body does not run at all. The loop exits immediately, as the condition check happens before running the body (see concept_flow).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of count at step 3?
A2
B4
C3
D1
💡 Hint
Check the 'count' column in execution_table row 3
At which step does the condition become true, causing the loop to exit?
AStep 4
BStep 2
CStep 3
DStep 1
💡 Hint
Look at the 'Condition Result' column in execution_table
If we change the condition to [ $count -ge 3 ], how many times will the loop run?
A1 time
B2 times
C3 times
D4 times
💡 Hint
Think about when the condition becomes true and compare with variable_tracker values
Concept Snapshot
until loop syntax:
until [ condition ]
do
  commands
  update variables
 done

Runs commands while condition is false.
Stops when condition becomes true.
Checks condition before each iteration.
Full Transcript
The until loop in bash runs the commands inside it repeatedly until the condition becomes true. It first checks the condition. If the condition is false, it runs the loop body. Then it updates variables and checks the condition again. This repeats until the condition is true, then the loop stops. For example, with count starting at 1, the loop prints count and increments it until count is greater than 3. When count reaches 4, the condition becomes true and the loop exits. Beginners often confuse until with while loops. Remember, until runs while the condition is false, stopping when true. Also, if the condition is true at the start, the loop body does not run at all.