shell脚本中的##是什么意思

use*_*199 4 bash shell-exec

在编译脚本时,我遇到了命令do f=${file##*/}.我很想知道##这一行意味着什么.感谢您提前回复

pax*_*blo 14

bash,它删除前缀模式.

在这里,它基本上为您提供了最后一个/路径分隔符后的所有内容(通过删除前缀*/,后跟任意数量的字符/):

pax> fspec=/path/to/some/file.txt

pax> echo ${fspec##*/}   # greedy remove */ at start
file.txt
Run Code Online (Sandbox Code Playgroud)

还有一个单#变量,它是一个非贪婪的匹配和后缀的等价物:

pax> echo ${fspec#*/}    # non-greedy remove */ at start
path/to/some/file.txt

pax> echo ${fspec%%/*}   # greedy remove /* at end

pax> echo ${fspec%/*}    # non-greedy remove /* at end
/path/to/some
Run Code Online (Sandbox Code Playgroud)

这些##*/%/*大致相当于你从中获得的内容basename和内容dirname,但在bash此之内你不必调用外部程序:

pax> basename $fspec
file.txt

pax> dirname $fspec
/path/to/some
Run Code Online (Sandbox Code Playgroud)