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

查看:72
本文介绍了花括号语法 ${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?

推荐答案

在这种情况下,它是一个 参数替换.

In this context, it is a parameter substitution.

${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" 产生等价物:

extractDir="abc.tar-tmp"

带有双 % 的替代符号:

The alternative notation with the double %:

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

将产生相当于:

extractDir="abc-tmp"

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

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

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

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