Reputation: 449
I want to extract filename by removing prefix and extension. e.g. file=foo_filename.txt
I am using this,but its not working.
${file#foo_%.txt}
Thanks
Upvotes: 0
Views: 377
Reputation: 7571
You got it almost right:
file=foo_filename.txt ; echo ${file%.txt}
Or for example in one step with sed:
file=foo_filename.txt ; echo ${file} | sed 's/^foo_\(.*\)\..*/\1/'
Yet another method:
file=foo_filename.txt ; basename -s .txt ${file#foo_}
You cannot "nest" variable expansion in bash: Can ${var} parameter expansion expressions be nested in bash?
Upvotes: 1
Reputation: 123410
file="foo_filename.txt"
file=${file#foo_}
file=${file%.*}
echo "$file"
Upvotes: 2