0
0
Bash Scriptingscripting~10 mins

Array length in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
Array length
📖 Scenario: You are working on a simple script to manage a list of fruits in your kitchen. You want to know how many different fruits you have at any time.
🎯 Goal: Build a Bash script that creates an array of fruits, sets a variable to the number of fruits in the array, and then prints that number.
📋 What You'll Learn
Create an array called fruits with exactly these values: apple, banana, cherry, date
Create a variable called count that stores the length of the fruits array
Print the value of count to show how many fruits are in the array
💡 Why This Matters
🌍 Real World
Counting items in a list is common in scripts that manage files, users, or data entries.
💼 Career
Knowing how to handle arrays and their lengths is essential for writing effective Bash scripts in system administration and automation.
Progress0 / 4 steps
1
Create the fruits array
Create an array called fruits with these exact values: apple, banana, cherry, date
Bash Scripting
Need a hint?

Use parentheses to create an array in Bash, like array=(item1 item2).

2
Set the count variable
Create a variable called count that stores the length of the fruits array using ${#fruits[@]}
Bash Scripting
Need a hint?

Use ${#array[@]} to get the length of an array in Bash.

3
Print the count
Use echo to print the value of the count variable
Bash Scripting
Need a hint?

Use echo $count to display the value stored in count.

4
Run the script and see the output
Run the script and confirm it prints 4, the number of fruits in the array
Bash Scripting
Need a hint?

The output should be the number 4 because there are four fruits in the array.