bash 中带有变量、大括号和哈希字符的 ${0##...} 语法是什么意思? [英] What is the meaning of the ${0##...} syntax with variable, braces and hash character in bash?
问题描述
我刚刚在 bash 中看到了一些我不太明白的代码.作为新手 bash 脚本编写者,我不确定发生了什么.
echo ${0##/*}回声 ${0}
我真的看不出这两个命令的输出有什么不同(打印脚本名称).那 #
只是一个评论吗?和 /*
有什么关系.如果是注释,它怎么不干扰结束 }
大括号?
谁能让我深入了解一下这种语法?
${string#substring}
从$string
前面删除substring
的最短匹配.
${string##substring}
从$string
前面删除substring
的最长匹配.
子串可能包含通配符*
,匹配所有内容.表达式 ${0##/*}
打印 $0
的值,除非它以正斜杠开头,在这种情况下它什么都不打印.
‡ 截至 2019 年 3 月 7 日,该指南错误地声称匹配是 $substring
,就好像 substring
是多变的.不是:substring
只是一个模式.
I just saw some code in bash that I didn't quite understand. Being the newbie bash scripter, I'm not sure what's going on.
echo ${0##/*}
echo ${0}
I don't really see a difference in output in these two commands (prints the script name). Is that #
just a comment? And what's with the /*
. If it is a comment, how come it doesn't interfere with the closing }
brace?
Can anyone give me some insight into this syntax?
See the section on Substring removal in the Advanced Bash-Scripting Guide‡:
${string#substring}
Deletes shortest match of
substring
from front of$string
.${string##substring}
Deletes longest match of
substring
from front of$string
.
The substring may include a wildcard *
, matching everything. The expression ${0##/*}
prints the value of $0
unless it starts with a forward slash, in which case it prints nothing.
‡ The guide, as of 3/7/2019, mistakenly claims that the match is of $substring
, as if substring
was the name of a variable. It's not: substring
is just a pattern.
这篇关于bash 中带有变量、大括号和哈希字符的 ${0##...} 语法是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!