If I have an array with 5 elements, for example:[a][b][c][d][e]Using echo ${myarray[4]} I can see what it holds.But what if I didn't know the number of elements in a given array? Is there a way of reading the last element of an unknown length array?i.e. The first element reading from the right to the left for any array?I would like to know how to do this in bash.
Is there a way of reading the last element of an array with bash?
bash;array
You can just use a negative index ${myarray[-1]} to get the last element. You can do the same thing for the second-last, and so on; in Bash:If the subscript used to reference an element of an indexed array  evaluates to a number less than zero, it is interpreted as relative to  one greater than the maximum index of the array, so negative indices  count back from the end of the array, and an index of -1 refers to the  last element.The same also works for assignment. When it says expression it really means an expression; you can write in any arithmetic expression there to compute the index, including one that computes using the length of the array ${#myarray[@]} explicitly.