Using break and continue in Bash loops
📖 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 10Create a variable called
skip_num set to 5Use a
for loop to iterate over numbersUse
continue to skip printing the number equal to skip_numUse
break to stop the loop when the number is 8Print 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