花括号语法$ {var%.*}是什么意思? [英] What does the curly-brace syntax ${var%.*} mean?

查看:187
本文介绍了花括号语法$ {var%.*}是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看一些旧代码,并遇到以下语法:

I was reviewing some of my old code and came across this syntax:

extractDir="${downloadFileName%.*}-tmp"

我发现搜索的唯一信息是命令列表,但这只是一个变量. 这种花括号语法在bash中意味着什么?

The only information I found searching refers to a list of commands, but this is just one variable. What does this curly-brace syntax mean in bash?

推荐答案

在这种情况下,它是 ${variable%.*}符号表示取值$variable,从值的尾部剥离模式.*(助记符:percenT的尾部有一个"t")并给出结果. (相比之下,${variable#xyz}表示从变量值的开头删除xyz-助记符:哈希在其头部带有'h'.)

The ${variable%.*} notation means take the value of $variable, strip off the pattern .* from the tail of the value — mnemonic: percenT has a 't' at the Tail — and give the result. (By contrast, ${variable#xyz} means remove xyz from the head of the variable's value — mnemonic: a Hash has an 'h' at the Head.)

给出:

downloadFileName=abc.tar.gz

评估extractDir="${downloadFileName%.*}-tmp"得出的结果等同于:

evaluating extractDir="${downloadFileName%.*}-tmp" yields the equivalent of:

extractDir="abc.tar-tmp"

带双%的替代表示法:

extractDir="${downloadFileName%%.*}-tmp"

产生的等价于:

extractDir="abc-tmp"

%%表示移除尽可能长的尾巴;相应地,##表示移除最长的匹配头.

The %% means remove the longest possible tail; correspondingly, ## means remove the longest matching head.

这篇关于花括号语法$ {var%.*}是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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