Bash 变量 ${0##*/}

bel*_*a83 8 shell-script

我试图了解${0##*/}我在 bash 脚本中遇到的变量。

我知道它$0包含脚本的名称或路径,然后##在${parameter##pattern}( source ) 中工作。

但我不明白/这里在做什么。我只知道这种带有两个斜杠的语法:${parameter/pat/string}

当我在 bash 中回显这个变量时,我得到bash:)

最后,我没有共享脚本的授权。我只想说变量被调用SOFT="${0##*/}"并在printf语句中使用"Error message sent by $SOFT"

Ant*_*hon 12

这将切断所有前面的路径元素,就像basename $0做的那样。在##试图找到前缀模式的最长匹配的扩展:

$ x=/a/b/c/d
$ echo ${x##*/}
d
$ basename $x
d
Run Code Online (Sandbox Code Playgroud)

从手册页:

${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.  
Run Code Online (Sandbox Code Playgroud)

使用的原因${0##*/}是它不涉及外部程序调用,但它有点模糊正在发生的事情。