Extract file basename without path and extension in bash [duplicate]

Given file names like these:

/the/path/foo.txt
bar.txt

I hope to get:

foo
bar

Why this doesn’t work?

#!/bin/bash

fullfile=$1
fname=$(basename $fullfile)
fbname=${fname%.*}
echo $fbname

What’s the right way to do it?

9 Answers
9

Leave a Comment