Bird
Raised Fist0
Bash Scriptingscripting~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does an until loop do in bash scripting?
easy
A. It repeats commands until the condition becomes true.
B. It repeats commands while the condition is true.
C. It runs commands only once regardless of condition.
D. It runs commands only if the condition is false.

Solution

  1. Step 1: Understand the until loop behavior

    An until loop runs repeatedly until its condition becomes true, meaning it stops when the condition is true.
  2. Step 2: Compare options with definition

    It repeats commands until the condition becomes true. correctly states the loop repeats until the condition is true. It repeats commands while the condition is true. describes a while loop. Options C and D are incorrect behaviors.
  3. Final Answer:

    It repeats commands until the condition becomes true. -> Option A
  4. Quick Check:

    until loop = repeat until true [OK]
Hint: Remember: until stops when condition is true [OK]
Common Mistakes:
  • Confusing until with while loop
  • Thinking until runs only once
  • Believing until runs while condition is true
2. Which of the following is the correct syntax for an until loop in bash?
easy
A. until [ condition ]; do commands; done
B. until ( condition ) { commands }
C. until { condition } do commands done
D. until condition do commands done

Solution

  1. Step 1: Recall bash until loop syntax

    The correct syntax uses square brackets for condition, followed by do and done keywords: until [ condition ]; do commands; done
  2. Step 2: Check each option

    until [ condition ]; do commands; done matches correct syntax. Options B, C, and D use incorrect brackets or missing semicolons.
  3. Final Answer:

    until [ condition ]; do commands; done -> Option A
  4. Quick Check:

    Syntax = until [ condition ]; do ... done [OK]
Hint: Use square brackets and do/done keywords [OK]
Common Mistakes:
  • Using parentheses instead of square brackets
  • Omitting do or done keywords
  • Missing semicolon before do
3. What will be the output of this script?
count=1
until [ $count -gt 3 ]
do
  echo $count
  ((count++))
done
medium
A. 1 2
B. 1 2 3 4
C. 1 2 3
D. No output, infinite loop

Solution

  1. Step 1: Analyze loop condition and increments

    The loop runs until count is greater than 3. Starting at 1, it prints count and increments it each time.
  2. Step 2: Trace iterations

    count=1 (prints 1), count=2 (prints 2), count=3 (prints 3), count=4 stops loop because 4 > 3.
  3. Final Answer:

    1 2 3 -> Option C
  4. Quick Check:

    Prints 1 to 3 before stopping [OK]
Hint: Loop stops when count > 3, so prints 1 to 3 [OK]
Common Mistakes:
  • Thinking it prints 4
  • Confusing until with while loop condition
  • Assuming infinite loop
4. Identify the error in this until loop script:
count=1
until [ $count -lt 5 ]
do
  echo $count
  ((count++))
done
medium
A. Missing semicolon after condition.
B. The loop will never run because condition is initially true.
C. The variable count is not initialized.
D. The condition should use -gt instead of -lt.

Solution

  1. Step 1: Understand the until loop condition

    until runs while the condition is false and stops when true. Here, condition is count -lt 5, which is true at start (count=1).
  2. Step 2: Analyze loop execution

    Since condition is true initially, loop does not run even once.
  3. Final Answer:

    The loop will never run because condition is initially true. -> Option B
  4. Quick Check:

    until stops immediately if condition true [OK]
Hint: until runs only while condition is false [OK]
Common Mistakes:
  • Confusing -lt with -gt in condition
  • Assuming loop runs at least once
  • Missing that until stops when condition true
5. You want to write a bash script that waits until a file named ready.txt exists before continuing. Which until loop correctly implements this?
hard
A. until [ ! -f ready.txt ]; do sleep 1; done
B. until [ ! -f ready.txt ]; do echo 'Waiting'; done
C. until [ -d ready.txt ]; do sleep 1; done
D. until [ -f ready.txt ]; do sleep 1; done

Solution

  1. Step 1: Understand the goal and condition

    The script should wait until the file exists. The loop runs while the condition is false, so condition should be "file exists" [ -f ready.txt ].
  2. Step 2: Check each option's condition

    until [ -f ready.txt ]; do sleep 1; done loops while [ -f ] is false (file missing), sleeping until file exists. B, C (-d for directory), and D use wrong conditions.
  3. Final Answer:

    until [ -f ready.txt ]; do sleep 1; done -> Option D
  4. Quick Check:

    Loop while file missing, stops when exists [OK]
Hint: Condition [ -f file ] stops when file exists [OK]
Common Mistakes:
  • Using -d instead of -f for file check
  • Using ! -f negation incorrectly
  • Not including sleep to avoid busy wait
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard