📖 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
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
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
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
Hint
The output should be the number 4 because there are four fruits in the array.
Practice
(1/5)
1. In bash scripting, which expression correctly gives the number of elements in an array named my_array?
easy
A. ${#my_array[@]}
B. ${my_array[@]}
C. ${#my_array}
D. ${my_array}
Solution
Step 1: Understand array length syntax
In bash, ${#array[@]} returns the count of elements in the array.
Step 2: Differentiate from other forms
${#array} returns the length of the first element, not the number of elements.
Final Answer:
${#my_array[@]} -> Option A
Quick Check:
Array length = ${#my_array[@]} [OK]
Hint: Use ${#array[@]} to get array size, not ${#array} [OK]
Common Mistakes:
Using ${#array} which gives length of first element
Using ${array[@]} which expands elements, not count
Confusing ${array} with array length
2. Which of the following is the correct syntax to store the length of a bash array files into a variable count?
easy
A. count=${files[@]}
B. count=${#files}
C. count=${#files[@]}
D. count=${files}
Solution
Step 1: Use correct length syntax
To get the number of elements, use ${#files[@]}.
Step 2: Assign to variable
Assigning with count=${#files[@]} stores the array length in count.
Final Answer:
count=${#files[@]} -> Option C
Quick Check:
Variable count = ${#files[@]} [OK]
Hint: Assign length with count=${#array[@]} to get element count [OK]
Common Mistakes:
Using count=${files[@]} which assigns all elements, not length
Using count=${#files} which gives length of first element
Forgetting to use ${} around the expression
3. What will be the output of this bash script?
arr=(apple banana cherry)
echo ${#arr[@]}
medium
A. apple
B. 18
C. 0
D. 3
Solution
Step 1: Understand array initialization
The array arr has 3 elements: apple, banana, cherry.
Step 2: Use ${#arr[@]} to get count
${#arr[@]} returns the number of elements, which is 3.
Final Answer:
3 -> Option D
Quick Check:
Array length = 3 [OK]
Hint: Count elements with ${#array[@]} to get number, not string length [OK]
Common Mistakes:
Confusing ${#arr[@]} with ${#arr} which gives length of first element
Expecting total string length instead of element count
Printing array without expansion
4. Identify the error in this bash script snippet that tries to print the number of elements in array data:
data=(1 2 3 4)
echo ${#data}
medium
A. It prints the length of the first element, not the array size
B. The array is not declared properly
C. The echo command is missing quotes
D. The syntax ${#data} is invalid in bash
Solution
Step 1: Check array length syntax
${#data} returns the length of the first element (string length), not the number of elements.
Step 2: Correct syntax for array length
Use ${#data[@]} to get the count of elements in the array.
Final Answer:
It prints the length of the first element, not the array size -> Option A
Quick Check:
Use ${#array[@]} for count, not ${#array} [OK]
Hint: Use ${#array[@]} to get count, ${#array} gives first element length [OK]
Common Mistakes:
Assuming ${#array} gives number of elements
Not using [@] inside the braces
Thinking echo needs quotes for this syntax
5. You have a bash array items that may be empty or have elements. You want to print "Empty" if it has zero elements, or "Count: N" where N is the number of elements. Which script snippet correctly does this?
hard
A. if [ ${#items} -eq 0 ]; then echo "Empty"; else echo "Count: ${#items[@]}"; fi
B. if [ ${#items[@]} -eq 0 ]; then echo "Empty"; else echo "Count: ${#items[@]}"; fi
C. if [ ${#items[@]} -eq 0 ]; then echo "Empty"; else echo "Count: ${#items}"; fi
D. if [ ${#items} -eq 0 ]; then echo "Empty"; else echo "Count: ${#items}"; fi
Solution
Step 1: Check condition for empty array
Use ${#items[@]} to get the number of elements. If zero, array is empty.
Step 2: Print count correctly
Use ${#items[@]} to print the count of elements, not ${#items} which is length of first element.
Final Answer:
if [ ${#items[@]} -eq 0 ]; then echo "Empty"; else echo "Count: ${#items[@]}"; fi -> Option B
Quick Check:
Use ${#array[@]} for count in both condition and output [OK]
Hint: Always use ${#array[@]} to check and print array length [OK]
Common Mistakes:
Using ${#array} instead of ${#array[@]} for count
Mixing length of first element with element count
Forgetting to use [ ] in if condition
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.