简体   繁体   中英

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?

What you want is ${file#*.} and ${file##*.} - ie '*.', 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM