Challenge - 5 Problems
Array Mastery in Bash
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output of this Bash array script?
Consider the following Bash script that uses arrays. What will it print?
Bash Scripting
arr=(apple banana cherry)
echo ${arr[1]}Attempts:
2 left
💡 Hint
Remember that Bash arrays start counting from zero.
✗ Incorrect
In Bash, arrays start at index 0. So arr[0] is 'apple', arr[1] is 'banana'.
🧠 Conceptual
intermediate2:00remaining
Why use arrays instead of separate variables in Bash?
Which reason best explains why arrays are useful for handling lists of data in Bash?
Attempts:
2 left
💡 Hint
Think about how you would keep track of many items easily.
✗ Incorrect
Arrays group related data so you can access each item by its position, making scripts cleaner and easier to manage.
🔧 Debug
advanced2:00remaining
Identify the error in this Bash array usage
What error will this script produce?
arr=(one two three)
echo ${arr[3]}
Attempts:
2 left
💡 Hint
Check the array length and valid indexes.
✗ Incorrect
The array has indexes 0,1,2. Index 3 is out of range, so Bash prints nothing (empty line).
🚀 Application
advanced2:00remaining
How to loop over all elements in a Bash array?
Which script correctly prints each element of the array 'fruits' on its own line?
Bash Scripting
fruits=(apple banana cherry)
Attempts:
2 left
💡 Hint
Use the syntax that preserves each array element as a separate word.
✗ Incorrect
Using "${fruits[@]}" expands each element separately, so the loop prints each fruit on its own line.
📝 Syntax
expert2:00remaining
Which option correctly declares and prints a Bash array element?
Select the option that correctly creates an array with elements 'red', 'green', 'blue' and prints the second element.
Attempts:
2 left
💡 Hint
Remember the correct syntax for Bash arrays uses parentheses without commas.
✗ Incorrect
Bash arrays use parentheses with space-separated values, no commas or braces. Indexing starts at 0.