花括号语法$ {var%.*}是什么意思? [英] What does the curly-brace syntax ${var%.*} mean?
问题描述
我正在查看一些旧代码,并遇到以下语法:
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?
推荐答案
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屋!