0
0
Bash Scriptingscripting~5 mins

break and continue in Bash Scripting - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the break command do in a Bash loop?
The break command immediately stops the loop and exits it, skipping any remaining iterations.
Click to reveal answer
beginner
What is the purpose of the continue command in a Bash loop?
The continue command skips the rest of the current loop iteration and moves to the next iteration.
Click to reveal answer
intermediate
How would you use break inside a for loop to stop when a condition is met?
Inside the loop, use an if statement to check the condition. If true, run break to exit the loop immediately.
Click to reveal answer
beginner
Explain the difference between break and continue in Bash loops.
break stops the entire loop immediately. continue skips only the current iteration and continues with the next one.
Click to reveal answer
advanced
Can break and continue be used in nested loops in Bash? How?
Yes. break and continue affect only the innermost loop by default. You can specify a number to affect outer loops, e.g., break 2 breaks two loops.
Click to reveal answer
What happens when break is executed inside a Bash loop?
ANothing happens; the loop continues normally.
BThe current iteration is skipped, and the loop continues.
CThe loop restarts from the beginning.
DThe loop stops immediately and exits.
Which command skips the rest of the current loop iteration and moves to the next one?
Acontinue
Bexit
Cstop
Dbreak
In a nested loop, what does break 2 do?
ABreaks only the innermost loop.
BBreaks two levels of loops, exiting both.
CSkips two iterations of the current loop.
DCauses an error.
What will this script print?<br>
for i in 1 2 3; do
  if [ "$i" -eq 2 ]; then
    continue
  fi
  echo $i
done
A1 3
B2 3
C1 2 3
D1 2
Which command would you use to stop a loop immediately when a condition is met?
Aexit
Bcontinue
Cbreak
Dskip
Describe how break and continue control the flow inside Bash loops.
Think about what happens to the loop after each command runs.
You got /3 concepts.
    Explain how to use break and continue in nested loops in Bash.
    Consider how many loops you want to exit or skip.
    You got /3 concepts.