If you want to get the last n characters of a string in bash, you can simply mix bash substrings${string:offset[:length]} – with bash string lengths – ${#string}. For example:

str="abcde"
n=3
echo ${str:${#str} - $n}

will print:

cde

  1. havingmysay says:

    I was looking for this, thanks, and then I found in my notes a simpler way:

    ${string:-n}

    A space after the colon will discard the output if it’s shorter than n characters.

  2. Joan says:

    ^ You forget the parenthesis, ${string:(-n)}, Parentheses or added space “escape” the position parameter.

  3. Jacques says:

    echo $string | tail -c n