0
0
Bash Scriptingscripting~15 mins

C-style for loop in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
Using C-style for loop in Bash scripting
📖 Scenario: You are managing a small inventory system using a Bash script. You want to count from 1 to 5 to simulate checking five items in stock.
🎯 Goal: Build a Bash script that uses a C-style for loop to count from 1 to 5 and print each number.
📋 What You'll Learn
Create a variable called start and set it to 1
Create a variable called end and set it to 5
Use a C-style for loop with i starting from start to end
Print the value of i inside the loop
💡 Why This Matters
🌍 Real World
Counting or iterating a fixed number of times is common in automation scripts, like processing files or checking system status.
💼 Career
Knowing how to write loops in Bash helps in system administration, DevOps, and automation roles where scripts manage repetitive tasks.
Progress0 / 4 steps
1
Set up start and end variables
Create a variable called start and set it to 1. Create another variable called end and set it to 5.
Bash Scripting
Need a hint?

Use start=1 and end=5 to create the variables.

2
Write the C-style for loop
Write a C-style for loop using i starting from start to end. Use the syntax for (( i=start; i<=end; i++ )).
Bash Scripting
Need a hint?

Use for (( i=start; i<=end; i++ )) and echo $i inside the loop.

3
Add the loop body to print numbers
Inside the for loop, write echo $i to print the current value of i.
Bash Scripting
Need a hint?

Use echo $i to print the number inside the loop.

4
Run the script and display output
Run the script and print the numbers from 1 to 5, each on its own line.
Bash Scripting
Need a hint?

Run the script using bash script.sh or execute it directly.