##在$ {}内意味着什么

时间:2013-01-25 14:11:56

标签: bash

我正在从github阅读shell脚本:script

它有两行代码让我困惑。我之前从未见过像#b这样的##。 任何人都可以向我解释这个,它是如何工作的?感谢。

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

注意:第一行产生类似'refs / heads / master'的内容 并且下一行删除前导refs / heads使branch_name成为master。

2 个答案:

答案 0 :(得分: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(但它似乎不支持链接到这个确切的文字;在页面中搜索##)。

答案 1 :(得分:2)

看看here,其中描述了许多其他字符串操作技巧。简而言之

${string##substring}

$substring前面删除$string的最长匹配。