📖 Scenario: You are writing a simple Bash script to process a list of numbers. You want to practice controlling the loop flow using break and continue commands.
🎯 Goal: Build a Bash script that loops through numbers 1 to 10, skips printing number 5, and stops the loop entirely when it reaches number 8.
📋 What You'll Learn
Create a Bash array called numbers containing numbers 1 to 10
Create a variable called skip_num set to 5
Use a for loop to iterate over numbers
Use continue to skip printing the number equal to skip_num
Use break to stop the loop when the number is 8
Print each number that is not skipped or stopped
💡 Why This Matters
🌍 Real World
Controlling loop flow with break and continue is useful in scripts that process data and need to skip or stop based on conditions, like filtering logs or processing files.
💼 Career
Many automation and system administration tasks require writing Bash scripts with loops that handle data efficiently using break and continue to manage flow.
Progress0 / 4 steps
1
Create the numbers array
Create a Bash array called numbers containing the numbers 1 to 10 exactly.
Bash Scripting
Hint
Use parentheses () to create an array in Bash.
2
Set the skip number variable
Create a variable called skip_num and set it to 5.
Bash Scripting
Hint
Assign the number 5 to the variable skip_num without spaces around the equal sign.
3
Write the loop with break and continue
Write a for loop using num to iterate over ${numbers[@]}. Inside the loop, use continue to skip when num equals skip_num. Use break to stop the loop when num equals 8. Print num if not skipped or stopped.
Bash Scripting
Hint
Use if statements with [ ] to compare numbers. Use continue to skip and break to stop the loop.
4
Print the final output
Run the script and print the numbers as per the loop logic. The output should show numbers 1 to 7, skipping 5, and stopping before 8.
Bash Scripting
Hint
The output should print numbers 1 to 7, skipping 5, and stop before 8.
Practice
(1/5)
1. What does the break command do inside a bash loop?
easy
A. Pauses the loop until user input
B. Stops the loop immediately and exits it
C. Restarts the loop from the beginning
D. Skips the current iteration and continues with the next
Solution
Step 1: Understand the role of break in loops
The break command immediately stops the loop and exits it, no matter where it is in the loop.
Step 2: Compare with other commands
continue skips to the next iteration, but break stops the loop entirely.
Final Answer:
Stops the loop immediately and exits it -> Option B
Quick Check:
break stops loop = D [OK]
Hint: Break means stop loop now, continue means skip one round [OK]
Common Mistakes:
Confusing break with continue
Thinking break skips iteration instead of stopping
Assuming break restarts the loop
2. Which of the following is the correct syntax to skip the current iteration in a bash for loop?
easy
A. break
B. skip
C. continue
D. next
Solution
Step 1: Identify the command to skip iteration
In bash, continue is used to skip the current iteration and move to the next one.
Step 2: Check other options
break stops the loop, skip and next are not valid bash commands.
Final Answer:
continue -> Option C
Quick Check:
Skip iteration = continue [OK]
Hint: Use continue to skip current loop round in bash [OK]
Common Mistakes:
Using break instead of continue
Using non-existent commands like skip or next
Confusing syntax with other languages
3. What will be the output of this bash script?
for i in 1 2 3 4 5; do
if [ "$i" -eq 3 ]; then
break
fi
echo $i
done
medium
A. 1 2 3 4 5
B. 3 4 5
C. 1 2 3
D. 1 2
Solution
Step 1: Analyze the loop and break condition
The loop runs from 1 to 5. When i equals 3, the break command stops the loop immediately.
Step 2: Determine which values are printed
Before reaching 3, the script echoes 1 and 2. When i is 3, loop breaks, so 3 is not printed.
Final Answer:
1 2 -> Option D
Quick Check:
Break stops at 3, prints before = 1 2 [OK]
Hint: Break stops loop before printing current value [OK]
Common Mistakes:
Including the break value in output
Confusing break with continue
Assuming loop runs fully
4. Identify the error in this bash script snippet:
for i in 1 2 3 4; do
if [ $i -eq 2 ]
continue
fi
echo $i
done
medium
A. Missing then keyword after if condition
B. Missing semicolon before continue
C. continue cannot be used inside if
D. echo command is misplaced
Solution
Step 1: Check if statement syntax
The if statement requires a then keyword after the condition before commands.
Step 2: Identify missing keyword
The script misses then after [ $i -eq 2 ], causing syntax error.
Final Answer:
Missing then keyword after if condition -> Option A
Quick Check:
If needs then before commands [OK]
Hint: Always put then after if condition in bash [OK]
Common Mistakes:
Omitting then after if
Assuming continue needs semicolon
Thinking continue can't be inside if
5. You want to print numbers 1 to 5 but skip 3 and stop the loop if number is greater than 4. Which code snippet achieves this?
hard
A. for i in 1 2 3 4 5; do
if [ "$i" -eq 3 ]; then continue; fi
if [ "$i" -gt 4 ]; then break; fi
echo $i
done
B. for i in 1 2 3 4 5; do
if [ "$i" -gt 4 ]; then continue; fi
if [ "$i" -eq 3 ]; then break; fi
echo $i
done
C. for i in 1 2 3 4 5; do
if [ "$i" -eq 3 ]; then break; fi
if [ "$i" -gt 4 ]; then continue; fi
echo $i
done
D. for i in 1 2 3 4 5; do
if [ "$i" -eq 3 ]; then continue; fi
if [ "$i" -gt 4 ]; then continue; fi
echo $i
done
Solution
Step 1: Understand requirements
Skip printing 3 (use continue), stop loop if number > 4 (use break).
Step 2: Analyze each option
for i in 1 2 3 4 5; do
if [ "$i" -eq 3 ]; then continue; fi
if [ "$i" -gt 4 ]; then break; fi
echo $i
done correctly uses continue to skip 3 and break to stop after 4. Others misuse break and continue or reverse conditions.
Final Answer:
Option A code snippet -> Option A
Quick Check:
continue skips 3, break stops after 4 [OK]
Hint: Use continue to skip, break to stop loop [OK]
Common Mistakes:
Swapping break and continue logic
Wrong condition order causing wrong output
Not stopping loop after number > 4
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.