Sometimes you will find that in bash you have to do a substitution in the value
of a variable. i.e. in my blogging scripts I have the filename in the variable
POSTFILE and for this posting POSTFILE has the value of 2007-05-28-12-34_string_replacements_in_bash.m4.
But when I have to generate a link to the post it should end in
.php
instead of
.m4. The naive approach would be to do something like:
echo $POSTFILE | sed "s/\.m4$/\.php/"
Bash however has a more elegant solution for this:
echo ${POSTFILE/%\.m4/.php}
Where % means to match at the end of the
POSTFILE variable. More
information about the syntax can be found in the bash man page in the Parameter
Expansion section.