How to remove last n characters from a string in Bash?

You can do like this (in bash v4 and higher):

#!/bin/bash

v="some string.rtf"

v2=${v::-4}

echo "$v --> $v2"

Note: macos uses bash 3.x by default

Leave a Comment