Count from the End of a Bash String with a Negative Offset
Count from the end of a string in Bash with ${s: -3}. The space before the minus sign matters, because ${s:-3} is the default-value expansion instead.
You want the last few characters of a string, and a negative offset looks like the obvious way to ask for them.
s=abcdef
echo "[${s: -3}]"
echo "[${s:-3}]"
echo "[${s: -3:2}]"
echo "[${s:0:-1}]"[def]
[abcdef]
[de]
[abcde]A negative offset counts back from the end of the value, but :- is also the spelling of the use-default-value expansion, so Bash needs the space to tell the two apart. Without it, ${s:-3} returns the value of s because the variable is set, which is why the second line prints the whole string. A negative length, as in the last line, stops that many characters before the end, as the substring expansion section explains.
A negative length needs Bash 4.2 or later. If the offset reaches back past the start of the string, the result is empty rather than an error. Parentheses around the number work too, as in ${s:(-3)}, if a bare space looks fragile in a long line.