0
0
Bash Scriptingscripting~10 mins

for loop with range ({1..10}) in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
Counting Numbers with a Bash For Loop
📖 Scenario: Imagine you want to count from 1 to 10 using a simple script. This is like counting steps as you walk up a staircase.
🎯 Goal: You will write a Bash script that uses a for loop with a range {1..10} to print numbers from 1 to 10, one number per line.
📋 What You'll Learn
Create a Bash script variable to hold a greeting message.
Create a variable to hold the range of numbers from 1 to 10.
Use a for loop with the range {1..10} to print each number.
Print the greeting message before the numbers.
💡 Why This Matters
🌍 Real World
Counting or looping through numbers is common in scripts that automate tasks like backups, monitoring, or batch processing files.
💼 Career
Knowing how to use loops and ranges in Bash is a basic skill for system administrators, DevOps engineers, and anyone automating tasks on Linux or Unix systems.
Progress0 / 4 steps
1
Create a greeting message variable
Create a Bash variable called greeting and set it to the exact text "Counting from 1 to 10:".
Bash Scripting
Need a hint?

Use variable_name="text" to create a string variable in Bash.

2
Create a variable for the number range
Create a Bash variable called numbers and set it to the range {1..10} exactly as numbers="{1..10}".
Bash Scripting
Need a hint?

Remember to put the range in quotes to assign it as a string.

3
Use a for loop with the range to print numbers
Write a for loop using for i in {1..10} to print each number i on its own line using echo.
Bash Scripting
Need a hint?

The loop starts with for i in {1..10}, then do, then the commands, and ends with done.

4
Print the greeting and the numbers
Add a line to print the greeting variable before the loop using echo "$greeting".
Bash Scripting
Need a hint?

Use echo "$greeting" before the loop to show the message.