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

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

问题描述

我检讨我的一些老code和跨越这种语法来了,但我忘了究竟是什么意思: - )

I was reviewing some of my old code and came across this syntax, but I forget exactly what it means :-)

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

我发现检索的唯一信息是指命令的列表,但是这仅仅是一个变量。 这是什么花括号语法的意思是在bash?

推荐答案

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

In this context, it is a parameter substitution.

$ {变量%。*} 表示取的值 $变量,脱光模式 * 从值的尾部。(助记:有百分之在尾部一个T),并给出了结果。 (相比之下, $ {变量#XYZ} 指自变量值的头部删除 XYZ ,因为(记忆)一个Hash具有在头一个H。)

The ${variable%.*} 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, because (mnemonic) a Hash has an 'h' at the Head.)

假设:

downloadFileName=abc.tar.gz
extractDir=abc.tar-tmp

另一种表示法:

extractDir=${downloadFileName%%.*}

收益

abc-tmp

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

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

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

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