Example: Remove the end comma
foo="foo, bar, baz,"
foo=${foo:0:-1}
Understanding the syntax
In the above example, it should be obvious that the syntax works like this
varname
:indexStart
:indexEnd
We use a negative number when we wish to truncate by rewinding from the end. We want everything except the last n characters, and of course can truncate in the other direction.
It's all pretty simple, once you are familiar with the synrax
You can only use the substring notation on strings declared as vairables.
Here are some examples
Printing characters 8 to 12 in the sring
$ addr="192.168.100.200" && echo ${addr:8:12}
100.200
Printing characters the first 7 characters
addr="192.168.100.200" && echo ${addr:0:+7}
192.168
Printing the last 8 characters
value="The Quick Brown Fox Jumped Over the Lazy Dog"
len=${#value}
echo ${value:$(($len-8)):$len}
Lazy Dog