0
0
Bash Scriptingscripting~10 mins

break and continue in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - break and continue
Start Loop
Check Condition
Yes
Inside Loop Body
If 'continue' condition?
YesSkip rest, go to next iteration
If 'break' condition?
YesExit Loop
Execute remaining code
Next iteration
Back to Check Condition
The loop starts and checks its condition. Inside the loop, if 'continue' is triggered, it skips to the next iteration. If 'break' is triggered, it exits the loop immediately. Otherwise, it runs the remaining code and repeats.
Execution Sample
Bash Scripting
for i in {1..5}; do
  if [ "$i" -eq 3 ]; then
    continue
  fi
  if [ "$i" -eq 4 ]; then
    break
  fi
  echo $i
done
This loop prints numbers 1 to 5 but skips 3 and stops completely at 4.
Execution Table
IterationiCondition 'i==3'ActionCondition 'i==4'ActionOutput
11FalseNo continueFalseNo break1
22FalseNo continueFalseNo break2
33TrueContinue (skip echo)N/AN/A
44FalseNo continueTrueBreak (exit loop)
💡 Loop exits at iteration 4 because break condition is met.
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
iN/A1234Loop exited
Key Moments - 3 Insights
Why is there no output when i equals 3?
At iteration 3 in the execution_table, the 'continue' condition is true, so the loop skips the echo command and moves to the next iteration without printing.
Why does the loop stop completely at i equals 4?
At iteration 4, the 'break' condition is true, so the loop exits immediately, stopping any further iterations as shown in the execution_table.
Does 'continue' stop the entire loop?
No, 'continue' only skips the rest of the current loop iteration and moves to the next one, unlike 'break' which exits the loop entirely, as seen in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output when i equals 3?
A3
BNo output
C4
DError
💡 Hint
Check the 'Action' column for iteration 3 where 'continue' skips the echo.
At which iteration does the loop stop running?
A2
B3
C4
D5
💡 Hint
Look at the 'Action' column for 'break' condition in the execution_table.
If we remove the 'continue' condition, what would be the output at iteration 3?
A3
BNo output
CLoop breaks
DError
💡 Hint
Without 'continue', the echo command runs normally at iteration 3 as per the variable_tracker.
Concept Snapshot
break and continue in bash loops:
- 'continue' skips the rest of current iteration, moves to next
- 'break' exits the loop immediately
- Use inside loops with if conditions
- Helps control loop flow clearly and simply
Full Transcript
This example shows a bash for loop from 1 to 5. When the variable i equals 3, the script uses 'continue' to skip printing 3 and move to the next number. When i equals 4, the script uses 'break' to stop the loop completely. The execution_table traces each iteration, showing variable values, conditions checked, actions taken, and output produced. The variable_tracker shows how i changes each step. Key moments clarify why output is missing at 3 and why the loop stops at 4. The quiz tests understanding of these behaviors by referencing the execution_table and variable_tracker.