Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does ## mean inside ${}

Tags:

bash

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.

like image 778
Zhenkai Avatar asked Apr 07 '26 17:04

Zhenkai


1 Answers

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

like image 127
Ignacio Vazquez-Abrams Avatar answered Apr 10 '26 21:04

Ignacio Vazquez-Abrams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!