Tuesday, September 18, 2012

How to strip file extension in a bash shell script function

FILENAME=`echo $1 | sed -e 's/\..*//g'`
From a BASH function take the first command line argument and strip off the extension and then assign it to the variable FILENAME.

It's probably not the best way of doing it, but it's how it happened.  I couldn't find any one liners from the googles :'(

Post your better solution in the comments below!11111!!!!

This is the total function (in my bash.bashrc):

mk430 () {
  FILENAME=`echo $1 | sed -e 's/\..*//g'`
  msp430-gcc -mmcu=msp430f2274 -o $FILENAME.elf $1
}
for compiling MSP430 C files to elfs, there's probably a better alias too!

2 comments: