Shell Scripting: File Names Without Extension

With the help of the curly braces notation, this is pretty easy to achieve:

# File Name Without Extension
${VAR%.*}

# File Extension
${VAR##*.}

And here’s a little example:

[valmont@DESKTOP ~]$ FNAME=itcode.script
[valmont@DESKTOP ~]$ echo ${FNAME##*.}
script
[valmont@DESKTOP ~]$ echo ${FNAME%.*}
itcode
[valmont@DESKTOP ~]$

Don’t tell anybody, I used to do it like this:

[valmont@DESKTOP-5KONCNE ~]$ FNAME=itcode.script
[valmont@DESKTOP-5KONCNE ~]$ echo $FNAME | sed -e ‘s#\.[^\.]*##’
itcode
[valmont@DESKTOP-5KONCNE ~]$

Ouch! This might look impressive. Not much else! Hahaha!


If you have any questions, feel free to ask in the comments section. Also, if this helped you in any way, please like and share. Until next time!

Leave a comment