Challenge - 5 Problems
Array Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output after adding an element to an array?
Consider the following Bash script that adds an element to an array. What will be the output?
Bash Scripting
arr=(apple banana cherry)
arr+=(date)
echo "${arr[@]}"Attempts:
2 left
💡 Hint
Remember that += adds elements to the end of the array.
✗ Incorrect
The += operator appends the new element 'date' to the existing array. So the array contains all four fruits.
💻 Command Output
intermediate2:00remaining
What happens when you remove an element from an array by index?
Given this Bash script, what will be the output?
Bash Scripting
arr=(red green blue yellow) unset arr[1] echo "${arr[0]} ${arr[1]} ${arr[2]} ${arr[3]}"
Attempts:
2 left
💡 Hint
unset removes the element but does not reindex the array.
✗ Incorrect
The element at index 1 ('green') is removed, but the array keeps its indices, so echo prints a blank space where the removed element was.
📝 Syntax
advanced2:00remaining
Which option correctly removes the first element from a Bash array?
You want to remove the first element from the array 'arr'. Which command does this correctly?
Bash Scripting
arr=(one two three four)
Attempts:
2 left
💡 Hint
Use array slicing to skip the first element.
✗ Incorrect
Option C uses slicing to create a new array starting from index 1, effectively removing the first element.
🔧 Debug
advanced2:00remaining
Why does this script fail to remove an element correctly?
Look at this script and find why it does not remove the element 'orange' from the array.
Bash Scripting
fruits=(apple orange banana) for i in "${!fruits[@]}"; do if [[ "${fruits[i]}" == "orange" ]]; then unset fruits[$i] fi done echo "${fruits[0]} ${fruits[1]} ${fruits[2]}"
Attempts:
2 left
💡 Hint
unset removes the element but does not shift the array indices.
✗ Incorrect
unset removes the element but leaves a gap in the array indices, so the output shows a blank space where 'orange' was.
🚀 Application
expert3:00remaining
How to add multiple elements to a Bash array in one command?
You have an array: arr=(cat dog). You want to add 'bird' and 'fish' at once. Which command does this correctly?
Attempts:
2 left
💡 Hint
Use += with parentheses to add multiple elements as separate items.
✗ Incorrect
Option A appends two separate elements 'bird' and 'fish' to the array correctly.