Challenge - 5 Problems
Array Access Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
Accessing a single element from a Bash array
What is the output of this Bash script?
Bash Scripting
arr=(apple banana cherry)
echo ${arr[1]}Attempts:
2 left
💡 Hint
Remember that Bash arrays start indexing at 0.
✗ Incorrect
In Bash, arrays start at index 0, so arr[1] is the second element, 'banana'.
💻 Command Output
intermediate2:00remaining
Accessing all elements of a Bash array
What does this script print?
Bash Scripting
arr=(red green blue)
echo ${arr[@]}Attempts:
2 left
💡 Hint
The '@' symbol expands all elements separated by spaces.
✗ Incorrect
Using ${arr[@]} prints all elements separated by spaces.
💻 Command Output
advanced2:00remaining
Accessing a range of elements from a Bash array
What is the output of this script?
Bash Scripting
arr=(one two three four five)
echo ${arr[@]:2:2}Attempts:
2 left
💡 Hint
The syntax ${arr[@]:start:length} extracts a slice.
✗ Incorrect
Starting at index 2, length 2, the elements are 'three' and 'four'.
💻 Command Output
advanced2:00remaining
Accessing the last element of a Bash array
What does this script output?
Bash Scripting
arr=(alpha beta gamma delta)
echo ${arr[-1]}Attempts:
2 left
💡 Hint
Negative indices are not supported in Bash arrays.
✗ Incorrect
Bash does not support negative indices; ${arr[-1]} is treated as an empty string.
💻 Command Output
expert2:00remaining
Accessing array elements with indirect referencing
What is the output of this script?
Bash Scripting
arr=(sun moon star)
index=1
echo ${arr[$index]}Attempts:
2 left
💡 Hint
Variables inside array indices are expanded normally.
✗ Incorrect
The variable index=1, so ${arr[$index]} accesses the second element 'moon'.