Zhenkai
Zhenkai

Reputation: 328

what does ## mean inside ${}

I am reading a shell scripts from github :script

It has two lines of code confused me. I have never seen ## used in bash like this before. could anyone explain this to me, how does it work? thanks.

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

Note:The first line produces something like 'refs/heads/master' and the next line remove the leading refs/heads make the branch_name becomes master.

Upvotes: 2

Views: 182

Answers (2)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798526

From the bash(1) man page, EXPANSION section, Parameter Expansion subsection:

   ${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.

Also available in the manual, of course (but it doesn't seem to support linking to this exact text; search the page for ##).

Upvotes: 7

Ivaylo Strandjev
Ivaylo Strandjev

Reputation: 70931

Have a look here where a lot other string manipulation tricks are described. In short

${string##substring}

Deletes longest match of $substring from front of $string.

Upvotes: 2

Related Questions