Getting part of an array

Arrays and collections / Arrays

numbers=(2 3 5 7 11)

#"${a[@]:start:count}" is a part of the array; it is always a copy
first2=("${numbers[@]:0:2}")
#first2 is (2 3)
last3=("${numbers[@]:2}")
#last3 is (5 7 11)

#a negative start counts from the end; the space before - is needed,
#because ${a[@]:-2} means "2 if the array is empty"
lastTwo=("${numbers[@]: -2}")
#lastTwo is (7 11)

#the copy does not see changes in the array
numbers[2]=50
#last3 is (5 7 11)

echo "first2 is (${first2[*]})"
echo "last3 is (${last3[*]})"
echo "lastTwo is (${lastTwo[*]})"