Shell scripts: remove the extension from a file name (bash)

Translation of the original in Catalan. Read it in: CatalàCastellano

When we write shell scripts we need to extract the extension of the files and we get used, at least in my case, to experimenting with the cut command and others like it trying to come up with some solution that allows us to do this. The hardest thing is to discover that you knew how to do it in a fast and clean way. That is what happened to me while reviewing an article that I had referenced myself: Shell Scripts.

Well then, getting to the point, to extract an extension, for example .jpg the only thing I have to do is this:

${FITXER%.jpg}

To make it clear, we could use it in this useful example:

#!/bin/sh
for FITXER in `ls *gif`
do
        F=`echo ${FITXER%.gif}`
        echo convert $F.gif $F.jpg
done

As you have surely discovered, what this small script allows us to do is convert all the *.gif files to *.jpg