Bash 变量 ${0##*/}

Bash 变量 ${0##*/}

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

我知道它$0包含脚本的名称或路径,然后##${parameter##pattern}(来源)。

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

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

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

答案1

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

$ x=/a/b/c/d
$ echo ${x##*/}
d
$ basename $x
d

从手册页:

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

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

相关内容