0
0
Bash Scriptingscripting~20 mins

Accessing array elements in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array Access Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2: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]}
Abanana
Bapple
Ccherry
DSyntaxError
Attempts:
2 left
💡 Hint
Remember that Bash arrays start indexing at 0.
💻 Command Output
intermediate
2:00remaining
Accessing all elements of a Bash array
What does this script print?
Bash Scripting
arr=(red green blue)
echo ${arr[@]}
Ared green blue
Bredgreenblue
Cred
DSyntaxError
Attempts:
2 left
💡 Hint
The '@' symbol expands all elements separated by spaces.
💻 Command Output
advanced
2: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}
ASyntaxError
Btwo three
Cthree
Dthree four
Attempts:
2 left
💡 Hint
The syntax ${arr[@]:start:length} extracts a slice.
💻 Command Output
advanced
2:00remaining
Accessing the last element of a Bash array
What does this script output?
Bash Scripting
arr=(alpha beta gamma delta)
echo ${arr[-1]}
Adelta
Balpha
Cempty line
DSyntaxError
Attempts:
2 left
💡 Hint
Negative indices are not supported in Bash arrays.
💻 Command Output
expert
2: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]}
Astar
Bmoon
Csun
DSyntaxError
Attempts:
2 left
💡 Hint
Variables inside array indices are expanded normally.