There are a couple different ways, but I usually end up using sed
For instance, try this on the command line:
$ echo "something.txt" | sed 's/\.txt$/.yo/'
Here's some of an explanation of what's happening:
sed looks at the input text ("something.txt" in this case) and tries to match ".txt" at the end
of the input. Side note:
the '\\' in front of the period with "txt" is necessary. If sed finds text that matches, it replaces
that text with ".yo". For this case, the '\\' is not
necessary for the period before "yo". The details of which are long. You should be able to read about why in the sed man page (man sed
) or with a Google search about regular expressions.
Anyway, the point is, the output of that command gives you the name of the file with the extension replaced. The command will work with any extention - you just have to replace "txt" for the extension you want to match and "yo" with the extension you want to replace it with.
If you want to work it into your script as a learning exercise, I'll hold off on posting how to integrate it. If you just want to "get it to work" then say so, and I'll show you how I'd work the command in.