什么是$在bash {0 ## ...}语法的变量,括号和散列字符的意义? [英] What is the meaning of the ${0##...} syntax with variable, braces and hash character in bash?
问题描述
我刚刚看到在bash一些code,我听不太懂。作为新手的bash脚本编写者,我不知道发生了什么事。
回声$ {0 ## / *}
回声$ {0}
我实在不明白,在这两个命令的输出(打印脚本名称)的差异。是#
只是一个评论?并与什么/ *
。如果是评论,为什么它不结束}
梅开二度干扰?
任何人都可以给我一些洞察到这种语法?
请参阅相关章节子串去除高级Bash脚本编程指南:
$ {串##串}
从$字符串前删除
子
的最长匹配。
块引用>*为通配符,匹配一切。命令
$ {0 ## / *}
打印$ 0
的值,除非它以正斜杠开始,在这种情况下,打印什么。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 longest match of
substring
from front of $string.The * is a wildcard, matching everything. The command
${0##/*}
prints the value of$0
unless it starts with a forward slash, in which case it prints nothing.这篇关于什么是$在bash {0 ## ...}语法的变量,括号和散列字符的意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!