Example: Remove the end comma

foo="foo, bar, baz,"

Understanding the syntax

In the above example, it should be obvious that the syntax works like this


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="" && echo ${addr:8:12}

Printing characters the first 7 characters

addr="" && echo ${addr:0:+7}

Printing the last 8 characters

value="The Quick Brown Fox Jumped Over the Lazy Dog"

echo ${value:$(($len-8)):$len}
Lazy Dog