0
0
Bash Scriptingscripting~15 mins

for loop (list-based) in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
Using a for loop to list favorite fruits
📖 Scenario: You want to tell your friends about your favorite fruits by listing them one by one.
🎯 Goal: Write a bash script that uses a for loop to go through a list of fruits and print each fruit on its own line.
📋 What You'll Learn
Create a list of fruits in a variable called fruits
Use a for loop to go through each fruit in fruits
Print each fruit inside the loop
💡 Why This Matters
🌍 Real World
Listing items and processing them one by one is common in scripts that manage files, users, or tasks.
💼 Career
Knowing how to use for loops in bash helps automate repetitive tasks in system administration and DevOps roles.
Progress0 / 4 steps
1
Create a list of fruits
Create a variable called fruits that contains these exact fruits separated by spaces: apple banana cherry
Bash Scripting
Need a hint?

Use quotes to assign multiple words to a variable in bash.

2
Set up a counter variable
Create a variable called count and set it to 0 to count how many fruits you print.
Bash Scripting
Need a hint?

Just write count=0 on a new line.

3
Use a for loop to go through the fruits
Write a for loop using the variable fruit to go through each item in $fruits. Inside the loop, increase count by 1.
Bash Scripting
Need a hint?

Use for fruit in $fruits; do ... done and count=$((count + 1)) inside.

4
Print each fruit inside the loop
Add a echo command inside the for loop to print the current fruit. Then print the total count after the loop.
Bash Scripting
Need a hint?

Use echo "$fruit" inside the loop and echo "Total fruits: $count" after.