iksemyonov
iksemyonov

Reputation: 4196

Bash parameter expansion and filename

I'm trying to use the recipe found in a comment on

Extract filename and extension in Bash

Namely, for

file="example.tar.gz"

I'm trying to do

echo "${file#.*}"

and

echo "${file##.*}"

but both return

example.tar.gz

as opposed to what's written in that comment (tar.gz and gz respectively).

Fedora 16 GNU bash, version 4.2.20(1)-release (x86_64-redhat-linux-gnu)

Shall I file a bug?

Upvotes: 0

Views: 203

Answers (2)

iksemyonov
iksemyonov

Reputation: 4196

Ohh sorry. Mixed up the position of the dot and the asterisk. Closing as the question is incorrect.

Upvotes: 0

ShankarG
ShankarG

Reputation: 1165

What you want is ${file#*.} and ${file##*.} - i.e. '*.', not '.*' The deletion is from the left, so you want it to stop at the full stop (whereas in your case it is looking for a . at the beginning of the string and failing). See this article on bash string operators for a very good and succinct explanation.

Upvotes: 2

Related Questions