marcus-g
10/13/2017 - 12:08 AM

Remove file extension with BASH

From StackOverflow: http://bit.ly/2gAVase

Here's how to do it with the # and % operators in Bash.

$ x="/foo/fizzbuzz.bar" $ y=${x%.bar} $ echo ${y##/} fizzbuzz ${x%.bar} could also be ${x%.} to remove everything after a dot or ${x%%.*} to remove everything after the first dot.

Example:

$ x="/foo/fizzbuzz.bar.quux" $ y=${x%.} $ echo $y /foo/fizzbuzz.bar $ y=${x%%.} $ echo $y /foo/fizzbuzz Documentation can be found in the Bash manual. Look for ${parameter%word} and ${parameter%%word} trailing portion matching section.

remove-file-extension(){
  local FILE_W_EXT=$1
  local FILE_WO_EXT=${FILE_W_EXT%.*}
  return FILE_WO_EXT
}