0
0
Bash Scriptingscripting~15 mins

break and continue in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
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 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
Need a 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
Need a 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
Need a 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
Need a hint?

The output should print numbers 1 to 7, skipping 5, and stop before 8.