What does the curly-brace syntax ${var%.*} mean?

天大地大妈咪最大 提交于 2020-01-19 05:19:08

问题


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

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

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?


回答1:


In this context, it is a parameter substitution.

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.)

Given:

downloadFileName=abc.tar.gz

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

extractDir="abc.tar-tmp"

The alternative notation with the double %:

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

would yield the equivalent of:

extractDir="abc-tmp"

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




回答2:


It indicates that parameter expansion will occur.




回答3:


It is used when expanding an environment variable adjacent to some text that is not the variable, so the shell does not include all of it in the variable name.



来源:https://stackoverflow.com/questions/9558986/what-does-the-curly-brace-syntax-var-mean

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!