📖 Scenario: You are working on a script that processes a list of fruits. You want to extract a part of this list to use it separately.
🎯 Goal: Learn how to create an array, set a starting index and length, slice the array using these values, and print the sliced array.
📋 What You'll Learn
Create an array with exact fruit names
Create variables for start index and length
Slice the array using the start index and length
Print the sliced array elements separated by spaces
💡 Why This Matters
🌍 Real World
Slicing arrays is useful when you want to process or analyze only part of a list, like extracting recent log entries or selecting a subset of data.
💼 Career
Many automation and scripting tasks require handling lists of items. Knowing how to slice arrays helps you write efficient scripts for data processing, system monitoring, and more.
Progress0 / 4 steps
1
Create the fruits array
Create an array called fruits with these exact elements in order: apple, banana, cherry, date, elderberry
Bash Scripting
Hint
Use parentheses and spaces to create the array: fruits=(apple banana cherry date elderberry)
2
Set start index and length variables
Create a variable called start and set it to 1. Create another variable called length and set it to 3.
Bash Scripting
Hint
Use simple variable assignments like start=1 and length=3
3
Slice the fruits array
Create a new array called sliced_fruits that contains a slice of the fruits array starting at index start and containing length elements.
Bash Scripting
Hint
Use array slicing syntax: sliced_fruits=(${fruits[@]:start:length})
4
Print the sliced array
Print the elements of the sliced_fruits array separated by spaces using echo.
Bash Scripting
Hint
Use echo ${sliced_fruits[@]} to print all elements separated by spaces.
Practice
(1/5)
1. In Bash scripting, what does array slicing do when you use ${array[@]:start:length}?
easy
A. Sorts the array elements alphabetically
B. Deletes elements from the array starting at start index
C. Extracts a part of the array starting at start index with length elements
D. Reverses the array elements from start to length
Solution
Step 1: Understand array slicing syntax
In Bash, ${array[@]:start:length} extracts elements from the array starting at index start and takes length elements.
Step 2: Compare options with this behavior
Only Extracts a part of the array starting at start index with length elements correctly describes this extraction. Other options describe unrelated operations.
Final Answer:
Extracts a part of the array starting at start index with length elements -> Option C
Quick Check:
Array slicing = Extract part [OK]
Hint: Remember slicing picks elements, not deletes or sorts [OK]
Common Mistakes:
Confusing slicing with deleting elements
Thinking slicing sorts or reverses the array
Mixing up start index and length meaning
2. Which of the following is the correct syntax to slice 3 elements from index 2 in a Bash array named arr?
easy
A. ${arr[2:3]}
B. ${arr[@]:2:3}
C. ${arr:2:3}
D. ${arr[2..4]}
Solution
Step 1: Recall correct array slicing syntax
In Bash, to slice an array, use ${array[@]:start:length}. The @ is needed to refer to all elements.
Step 2: Check each option
${arr[@]:2:3} matches the correct syntax: ${arr[@]:2:3}. ${arr:2:3} misses @, which is required for arrays. Options B and D use invalid syntax.
Final Answer:
${arr[@]:2:3} -> Option B
Quick Check:
Use ${array[@]:start:length} for slicing [OK]
Hint: Use @ inside braces for array slicing syntax [OK]
Common Mistakes:
Omitting @ when slicing arrays
Using square brackets with colon syntax
Trying to slice with invalid range notation
3. Given the Bash array arr=(a b c d e), what is the output of echo ${arr[@]:1:3}?
medium
A. a b c
B. c d e
C. b c d e
D. b c d
Solution
Step 1: Identify the array and slice parameters
The array is (a b c d e). The slice starts at index 1 and takes 3 elements.
Step 2: Extract elements from index 1 to 3
Index 0 = a, 1 = b, 2 = c, 3 = d, 4 = e. Taking 3 elements from index 1 gives: b, c, d.
Final Answer:
b c d -> Option D
Quick Check:
Slice from 1 length 3 = b c d [OK]
Hint: Count from zero, pick length elements starting at start [OK]
Common Mistakes:
Starting count at 1 instead of 0
Taking wrong number of elements
Confusing output with full array
4. What is wrong with this Bash code snippet for slicing an array?
arr=(x y z)
echo ${arr:1:2}
medium
A. It will output an empty string
B. It will output only one element instead of two
C. It will cause a syntax error because slicing is not allowed
D. It will output 'y z' correctly
Solution
Step 1: Check slicing syntax correctness
The code uses ${arr:1:2} without @. For arrays, @ is needed to slice multiple elements.
Step 2: Understand the effect of missing @
Without @, Bash uses the first element x as a scalar string and slices it from index 1 length 2. Since x has length 1, it outputs an empty string.
Final Answer:
It will output an empty string -> Option A
Quick Check:
Missing @ slices first elem as string -> empty [OK]
Hint: Always use @ for array slicing, else string slicing happens [OK]
Common Mistakes:
Omitting @ and expecting array slice
Assuming no error but wrong output
Confusing string and array slicing syntax
5. You have a Bash array arr=(1 2 3 4 5). You want to extract the last 3 elements using array slicing. Which command will do this correctly?
hard
A. echo ${arr[@]:-3}
B. echo ${arr[@]: -3:3}
C. echo ${arr[@]:3:3}
D. echo ${arr[@]:3}
Solution
Step 1: Understand negative index slicing in Bash
Using a negative start index slices from the end. ${arr[@]:-3} extracts from 3rd from end to the end (last 3 elements).
Step 2: Check each option
${arr[@]: -3:3} has space after : causing bad substitution. ${arr[@]:3:3} slices from index 3 length 3 but only 2 elements remain (4 5). ${arr[@]:3} slices from 3 to end (4 5). Only ${arr[@]:-3} gets exactly the last 3: 3 4 5.
Final Answer:
echo ${arr[@]:-3} -> Option A
Quick Check:
Negative start index slices from end [OK]
Hint: Use negative start index without length to get last elements [OK]
Common Mistakes:
Adding space after ':' before negative offset
Using slice length larger than remaining elements
Not knowing negative indices slice from array end
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.