Bash last char of a string

Bash shell is the common linux command language.
Post Reply
chris
Site Admin
Posts: 177
Joined: Mon Jul 21, 2008 9:52 am

Bash last char of a string

Post by chris »

Hi,

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

Thanks
chris
Site Admin
Posts: 177
Joined: Mon Jul 21, 2008 9:52 am

Re: Bash last char of a string

Post by chris »

Found a resolution:

This gives the last character:

Code: Select all

echo "${FILENAME:$((${#str}-1)):1}"
chris
Site Admin
Posts: 177
Joined: Mon Jul 21, 2008 9:52 am

Re: Bash last char of a string

Post 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 ...)
Post Reply