#Linux : How to Slice an Array in #Bash
A lot of things can be done using just the command line.
In fact, Bash shell scripting language is a touring-complete language, so anything can be done!
One important feature is the ability to slice an array (i.e. select a contiguous subset of elements of a collection).
So let’s say for example we stored the list of installed packages into a variable
PACKAGE_LIST as a bash array:
and for some reason we want to select elements from 4 to 10:
Let me explain. Here, we are using Bash parameter expansion:
[@]following the array name returns the whole content of the array.
:X:Ypart is doing the slicing by taking a slice of length
Ystarting at position
X. Note that if
Xis negative, that is we start at
Xelements from the end, we must put a space between the colon and the number.