关于shell:变量替换如何在Bash中提取当前文件夹的名称?

How does variable substitution to extract the current folder's name work in Bash?

我使用下面的脚本获取bash中当前文件夹的名称:

1
${PWD##*/}

这是我在这个问题中看到的一个技巧。然而,我并不真正理解语法##*/的含义(除了*/可能表示"斜杠以内的任何字符")。

有人能解释一下吗?如果这是一个愚蠢的问题,请提前道歉!


GNU的bash手册从https://www.gnu.org /软件/ bash手册/ HTML /节点/ shell-parameter-expansion.html _

${parameter#word}
${parameter##word}

The word is expanded to produce a pattern just as in filename expansion (see Filename Expansion). If the pattern matches the beginning of the expanded 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.

简单的解释

假设,参数,/home/abc/pqrPwd =

Word是一*/比赛模式。这意味着/home/abc/。因为pqr有一个尾随doesnot /pqrdoesnot是模式匹配。

和GNU的bash手册

the longest matching pattern (the ‘##’ case) deleted

##均值最长的匹配模式和删除参数。这意味着从/home/abc/pqr/home/abc/删除。

这给你pqr

PS:没有愚蠢的问题