0
0
Bash Scriptingscripting~10 mins

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

Choose your learning style9 modes available
Concept Flow - while loop
Initialize variable
Check condition
|Yes
Execute loop body
Update variable
Back to Check
Exit loop
Back to Check
The while loop starts by checking a condition. If true, it runs the loop body, updates variables, then checks again. It stops when the condition is false.
Execution Sample
Bash Scripting
count=1
while [ $count -le 3 ]
do
  echo "Count is $count"
  ((count++))
done
This script prints the count from 1 to 3 using a while loop.
Execution Table
Stepcount valueCondition [ $count -le 3 ]ActionOutput
11TruePrint 'Count is 1', increment count to 2Count is 1
22TruePrint 'Count is 2', increment count to 3Count is 2
33TruePrint 'Count is 3', increment count to 4Count is 3
44FalseExit loop
💡 count reaches 4, condition 4 <= 3 is False, loop stops
Variable Tracker
VariableStartAfter 1After 2After 3Final
count12344
Key Moments - 2 Insights
Why does the loop stop when count is 4 even though the loop body increments count after printing?
The condition is checked before the loop body runs each time. When count is 4, the condition [ $count -le 3 ] is false, so the loop exits before printing or incrementing again, as shown in step 4 of the execution_table.
What happens if we forget to increment count inside the loop?
If count is never incremented, the condition stays true forever, causing an infinite loop. The execution_table shows count changing each step; without that, the loop never reaches the exit condition.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of count at step 3?
A4
B2
C3
D1
💡 Hint
Check the 'count value' column in row 3 of the execution_table.
At which step does the condition become false and the loop stops?
AStep 4
BStep 3
CStep 2
DStep 1
💡 Hint
Look at the 'Condition' column in the execution_table and find where it says False.
If we start count at 5 instead of 1, what happens to the loop?
AIt runs 3 times
BIt does not run at all
CIt runs once
DIt runs infinitely
💡 Hint
Refer to the variable_tracker and execution_table logic: condition is checked before loop body.
Concept Snapshot
while loop syntax in bash:
while [ condition ]
do
  commands
  update variables
 done

The loop runs as long as the condition is true.
Condition is checked before each iteration.
Remember to update variables to avoid infinite loops.
Full Transcript
This visual execution shows how a bash while loop works. We start with count=1. The loop checks if count is less or equal to 3. If yes, it prints the count and increments it. This repeats until count becomes 4, when the condition fails and the loop stops. The variable_tracker shows count changing from 1 to 4 step by step. Key points: the condition is checked before running the loop body, and forgetting to update count causes an infinite loop. The quiz questions help check understanding of count values and loop stopping conditions.