0
0
Bash Scriptingscripting~10 mins

Array slicing in Bash Scripting - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to extract the first three elements from the array.

Bash Scripting
arr=(apple banana cherry date)
slice=(${arr[@]:[1]:3})
echo "${slice[@]}"
Drag options to blanks, or click blank then click option'
A2
B1
C0
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Starting the slice at 1 will skip the first element.
Using a length other than 3 will give wrong number of elements.
2fill in blank
medium

Complete the code to extract elements starting from the second element to the end.

Bash Scripting
arr=(red green blue yellow)
slice=(${arr[@]:[1])
echo "${slice[@]}"
Drag options to blanks, or click blank then click option'
A0
B1
C3
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Starting at 0 extracts the whole array.
Starting at 2 skips the second element.
3fill in blank
hard

Fix the error in the code to correctly slice the array from the third element, taking two elements.

Bash Scripting
arr=(one two three four five)
slice=(${arr[@]:[1]:2})
echo "${slice[@]}"
Drag options to blanks, or click blank then click option'
A2
B1
C3
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 3 starts from the fourth element, not third.
Using index 1 starts from the second element.
4fill in blank
hard

Fill both blanks to slice the array from the second element and take three elements.

Bash Scripting
arr=(cat dog bird fish horse)
slice=(${arr[@]:[1]:[2])
echo "${slice[@]}"
Drag options to blanks, or click blank then click option'
A1
B2
C3
D4
Attempts:
3 left
💡 Hint
Common Mistakes
Starting at 2 skips the second element.
Taking 2 elements returns fewer than needed.
5fill in blank
hard

Complete the code to extract the last three elements from the array using a negative index.

Bash Scripting
arr=(apple bat cherry dog elephant fox)
slice=(${arr[@]:[1])
echo "${slice[@]}"
Drag options to blanks, or click blank then click option'
A3
B-1
C0
D-3
Attempts:
3 left
💡 Hint
Common Mistakes
Using 3 extracts from index 3 to end, which happens to be the same here, but positive indexes count from the start.
Using 0 extracts the entire array.
Using -1 extracts only the last element.