简体   繁体   English

Bash参数扩展和文件名

[英]Bash parameter expansion and filename

I'm trying to use the recipe found in a comment on 我正在尝试使用评论中找到的食谱

Extract filename and extension in Bash 在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). 而不是该评论中所写的内容(分别为tar.gz和gz)。

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

Shall I file a bug? 我要提交一个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). 你想要的是$ {file#*。}和$ {file ## *。} - 即'*。',而不是'。*'删除是从左边开始的,所以你希望它在句号停止(而在你的情况下,它是在字符串的开头寻找一个。并且失败了)。 See this article on bash string operators for a very good and succinct explanation. 有关非常好和简洁的解释,请参阅有关bash字符串运算符的这篇文章

Ohh sorry. 噢抱歉。 Mixed up the position of the dot and the asterisk. 将点和星号的位置混合在一起。 Closing as the question is incorrect. 关闭,因为问题是不正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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