bash – remove last char(s) from string


The last character of a string is very easy to remove in bash.

out="remove last char!"
echo "${out%?}"

returns
remove last char

To remove the last two characters:

out="remove last char!"
echo "${out%%??}"

returns
remove last cha

or use
out="remove last char!"
echo ${out:0:$((${#out})) - 2}

to also get
remove last cha

Leave a comment

Your email address will not be published. Required fields are marked *

I confirm