0
0
Bash Scriptingscripting~20 mins

Array slicing in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array Slicing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this Bash array slicing?
Given the array arr=(a b c d e f g), what is the output of echo ${arr[@]:2:3}?
Bash Scripting
arr=(a b c d e f g)
echo ${arr[@]:2:3}
Ad e f
Bb c d
Ca b c
Dc d e
Attempts:
2 left
💡 Hint
Remember that array slicing starts at index 0 and the syntax is ${array[@]:start:length}.
💻 Command Output
intermediate
2:00remaining
What does this negative index slice output?
With arr=(1 2 3 4 5 6 7 8 9), what is the output of echo ${arr[@]: -4:2}?
Bash Scripting
arr=(1 2 3 4 5 6 7 8 9)
echo ${arr[@]: -4:2}
A8 9
B6 7
C7 8
D5 6
Attempts:
2 left
💡 Hint
Negative start index counts from the end of the array.
📝 Syntax
advanced
2:00remaining
Which option correctly slices the last 3 elements of an array?
Given arr=(x y z a b c d), which command outputs the last 3 elements?
Bash Scripting
arr=(x y z a b c d)
Aecho ${arr[@]: -3:3}
Becho ${arr[@]:3:3}
Cecho ${arr[@]: -3}
Decho ${arr[@]: -4:3}
Attempts:
2 left
💡 Hint
Use negative start index and specify length to get exact elements.
🔧 Debug
advanced
2:00remaining
Why does this slicing command fail?
Given arr=(10 20 30 40 50), why does echo ${arr[@]:-3:2} produce unexpected output?
Bash Scripting
arr=(10 20 30 40 50)
echo ${arr[@]:-3:2}
ABecause the syntax ${arr[@]:-3:2} is interpreted as a default value, not slicing
BBecause negative indices must have a space after the colon to be recognized
CBecause there is no space after the colon before the negative index
DBecause the array is too short for this slice
Attempts:
2 left
💡 Hint
Check how Bash interprets the colon and minus sign in parameter expansion.
🚀 Application
expert
3:00remaining
How many elements are in the slice?
Given arr=(alpha beta gamma delta epsilon zeta eta), what is the number of elements output by echo ${arr[@]:4:10}?
Bash Scripting
arr=(alpha beta gamma delta epsilon zeta eta)
echo ${arr[@]:4:10}
A4
B7
C3
D10
Attempts:
2 left
💡 Hint
The slice length can be longer than the remaining elements, but output only includes available elements.