Sometimes we only need to operate on a slice of an array - a set of contiguous elements.
Shell parameter expansion is our friend.
Using our Binary array from here, what can we do?
The syntax we need is ${string:<start>:<count>}.
#!/usr/bin/env bash
# arrays subset
clear
echo "bashMath2.sh"
echo
declare -a Binary # make Binary an array indexed by integers
Binary=(1 2 4 8 16 32 64 128)
for t in ${Binary[@]}; do
echo $t
done
printf '%s' "The 6th element in Binary is: "
echo ${Binary[@]:5:1}
echo
echo "Now the 3rd, 4th and 5th elements: "
echo ${Binary[@]:2:3}
echo
echo "Rememember: string indexing starts at 0"
echo
bashMath2.sh 1 2 4 8 16 32 64 128 The 6th element in Binary is: 32 Now the 3rd, 4th and 5th elements: 4 8 16 Rememember: string indexing starts at 0... just what we asked for.