Page 1 of 1

Bash last char of a string

Posted: Sun Sep 02, 2018 7:11 pm
by chris
Hi,

does anyone know how to get the last characters of a string?
in bash.

Thanks

Re: Bash last char of a string

Posted: Sun Sep 02, 2018 7:31 pm
by chris
Found a resolution:

This gives the last character:

Code: Select all

echo "${FILENAME:$((${#str}-1)):1}"

Re: Bash last char of a string

Posted: Sat Sep 19, 2020 2:46 pm
by chris
you can also use:

Code: Select all

echo ${STRING: -1}
Just remember that the last character can sometimes be a whitespace ( space, newline ...)