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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:08:01  来源:igfitidea点击:

percent symbol in Bash, what's it used for?

bash

提问by Senthil Kumaran

I have a filename which ends with .zipand 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 %*.zipis removing my extension?

这里发生了什么?为什么要%*.zip删除我的扩展程序?

回答by pfnuesel

Delete the shortest match of stringin $varfrom the beginning:

从头开始删除stringin的最短匹配项$var

${var#string}

Delete the longest match of stringin $varfrom the beginning:

从头开始删除stringin的最长匹配项$var

${var##string}

Delete the shortest match of stringin $varfrom the end:

从末尾删除stringin的最短匹配项$var

${var%string}

Delete the longest match of stringin $varfrom the end:

从末尾删除stringin的最长匹配项$var

${var%%string}

Try:

尝试:

var=foobarbar
echo "${var%b*r}"
> foobar
echo "${var%%b*r}"
> foo