Mailinglist Archive: opensuse (686 mails)

< Previous Next >
Re: [opensuse] bash: how to get file extension
Hello,

On Mon, 15 Jul 2013, Carlos E. R. wrote:
I found this:

# (from /usr/local/bin/rosegarden-audiofile-importer)

base=`basename "$infile"`

*tsk* ;)

stem=${base%.*}
extension=${base##*.}

For years I have (commented, just to remember)

# alias basename='echo ${1##*\/}'
# alias dirname='echo ${1%\/*}'

in my .bashrc. I.e. the above should be:

base="${infile##*\/}"
stem="${base%.*}"
extension="${base##*.}"

The general pattern is:

Use "${var##*C}" to get stuff after the last C
use "${var#*C}" to get stuff after the first C
use "${var%C*}" to get stuff before the last C
use "${var%%C*}" to get stuff before the first C

HTH,
-dnh

--
"Amnesia used to be my favorite word, but then I forgot it."
-- the BSD fortune file
--
To unsubscribe, e-mail: opensuse+unsubscribe@xxxxxxxxxxxx
To contact the owner, e-mail: opensuse+owner@xxxxxxxxxxxx

< Previous Next >