什么是大括号语法$ {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.