Bash中的百分比符号,它的作用是什么? [英] percent symbol in Bash, what's it used for?

查看:89
本文介绍了Bash中的百分比符号,它的作用是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个以.zip结尾的文件名,我只想要没有zip的文件名.在这里,我发现了bash的一个窍门.

I have a filename which ends with .zip and I wanted just the filename without zip. Here I found a trick in bash.

$f="05 - Means-End Analysis Videos.zip"
$echo "${f%*.zip}"
05 - Means-End Analysis Videos

这是怎么回事? %*.zip为何要删除我的扩展名?

What is happening here? How come %*.zip is removing my extension?

推荐答案

从开头删除$varstring的最短匹配项:

Delete the shortest match of string in $var from the beginning:

${var#string}

从开头删除$varstring的最长匹配项:

Delete the longest match of string in $var from the beginning:

${var##string}

从末尾删除$varstring的最短匹配项:

Delete the shortest match of string in $var from the end:

${var%string}

从末尾删除$varstring的最长匹配项:

Delete the longest match of string in $var from the end:

${var%%string}

尝试:

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

这篇关于Bash中的百分比符号,它的作用是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆