2.4 Arrays
Arrays are a commonplace feature in virtually all programming and scripting languages. What they primarily offer us as users is the ability to store multiple values within the same variable in sequence with each other. This can be used in conjunction with loops to perform similar actions over a collection of data.
We can define a simple array and read back the contents in a simple manner like so:
# Defines an array of 5 elements
array=(hello world how are you?)
# Prints all elements in the array using a parameter expansion
echo ${array[@]}
To read back specific values from an array we simply need to apply an explicit index value. Alternatively we can retrieve a sub-array of the provided data:
# Multi-value array
array=(1 2 3 4 5)
# Print the 3rd element
echo ${array[2]}
# Get all elements between indexes 2 - 4
echo ${array[@]:1:3}