How to find the last element in an array in unix? How to find the last element in an array in unix? unix unix

How to find the last element in an array in unix?


I think that something like that could make it. It is not very nice, I know, but cannot think in other good ways:

#!/bin/basha=("hello" "bye" "another" "word")i=0num_words=${#a[@]}echo "there are $num_words words"for word in "${a[@]}"do        let i=i+1        echo $i $word        if [ $i -eq $num_words ]; then                echo "last word!"        fidone

Test

$ ./testthere are 4 words1 hello2 bye3 another4 wordlast word!


$ set -A arr 1 2 3 4 5$ let LAST_ELEMENT=${#arr[*]}-1$ echo ${arr[$LAST_ELEMENT]}