Bash 中的百分比符号,它是做什么用的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34951901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
percent symbol in Bash, what's it used for?
提问by Senthil Kumaran
I have a filename which ends with .zip
and I wanted just the filename without zip. Here I found a trick in bash.
我有一个以 结尾的文件名,.zip
我只想要没有 zip 的文件名。在这里,我在 bash 中找到了一个技巧。
$f="05 - Means-End Analysis Videos.zip"
$echo "${f%*.zip}"
05 - Means-End Analysis Videos
What is happening here? How come %*.zip
is removing my extension?
这里发生了什么?为什么要%*.zip
删除我的扩展程序?
回答by pfnuesel
Delete the shortest match of string
in $var
from the beginning:
从头开始删除string
in的最短匹配项$var
:
${var#string}
Delete the longest match of string
in $var
from the beginning:
从头开始删除string
in的最长匹配项$var
:
${var##string}
Delete the shortest match of string
in $var
from the end:
从末尾删除string
in的最短匹配项$var
:
${var%string}
Delete the longest match of string
in $var
from the end:
从末尾删除string
in的最长匹配项$var
:
${var%%string}
Try:
尝试:
var=foobarbar
echo "${var%b*r}"
> foobar
echo "${var%%b*r}"
> foo