Shell script parameter replacement

Can someone tell me what is the difference between these two conventions to get the name of the current script?

#!/bin/sh

echo $0
echo ${0##*/}

Does the second version share the path on behalf of the script?

Thank!

+3
source share
2 answers

Your guess is correct. From the docs bashon my machine:

   $ {parameter # word}
   $ {parameter ## word}
          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 pattern (the ``##'' case) deleted.
          If parameter is @ or *, the pattern removal operation is applied
          to  each  positional parameter in turn, and the expansion is the
          resultant list.  If parameter is an array  variable  subscripted
          with  @  or  *, the pattern removal operation is applied to each
          member of the array in turn, and the expansion is the  resultant
          list.
+2

, bash $0, */regexp, , $0 - "/bin/bash", ${0 ## */} "bash". `basename $0` .

+1

Source: https://habr.com/ru/post/1759124/


All Articles