Bash get last line from a variable
If you want an array with one element per line from STRING
, use
readarray -t lines <<< "$STRING"
Then, the first line would be ${lines[0]}
, and the last line would be ${lines[-1]}
. In older versions of bash
, negative indices aren't allowed and you'll have to compute the last index manually: ${lines[${#lines[@]}-1]}
.