2013-01-25 287 views
2

我从GitHub读取shell脚本:script

它的两行代码搞糊涂了。我以前从来没有见过像这样用过bash的##。 任何人都可以向我解释这个,它是如何工作的?谢谢。

branch_name=$(git symbolic-ref -q HEAD) 
branch_name=${branch_name##refs/heads/} 

注意:第一生产线生产类似“裁判/头/硕士 下一行删除前导裁判/头让branch_name成为主。

回答

7

bash(1)手册页,扩展部分,参数扩展款:

${parameter#word} 
    ${parameter##word} 
      Remove matching prefix pattern. The word is expanded to produce 
      a pattern just as in pathname expansion. If the pattern matches 
      the beginning of the value of parameter, then the result of the 
      expansion is the expanded value of parameter with the shortest 
      matching pattern (the ``#'' case) or the longest matching pat‐ 
      tern (the ``##'' case) deleted. 

也可in the manual,当然,(但它似乎不支持链接到这个确切的文本;搜索##的页面)。

2

看一看here其中描述了很多其他字符串操作技巧。总之

${string##substring} 

删除从$string$substring最长匹配。