0
0
Bash Scriptingscripting~15 mins

Array slicing in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
Array slicing in Bash scripting
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use echo ${sliced_fruits[@]} to print all elements separated by spaces.