Variable substitution

Cutting off Strings at the end of a Variable

STRIPPED_VAR=${VAR%pattern}

e.g:

$ VAR=abc.def.ghi
$ echo ${VAR%.*}
abc.def

To make it greedy use %%:

$ echo ${VAR%%.*}
abc

Cutting off Strings at the beginning of a Variable

STRIPPED_VAR=${VAR#pattern}`

e.g:

$ VAR=abc.def.ghi
$ echo ${VAR#*.}
def.ghi

To make it greedy use ##:

$ echo ${VAR##*.}
ghi

Replacing String occurrences in Variable

To replace the first occurrence of a pattern:

$ NEW_VAR=${VAR/search/replace}
$ VAR=abc.def.ghi
$ echo ${VAR/def/-}
abc.-.ghi

To replace all occurrences of a pattern:

$ NEW_VAR=${VAR//search/replace}
$ VAR=adc.def.gdi
$ echo ${VAR//d/x}
axc.xef.gxi