根据man bash
:
${parameter#word} ${parameter##word} Remove matching prefix pattern. The word is expanded to produce a pattern just as in pathname expansion, and matched against the expanded value of parameter using the rules described under Pattern Matching below. 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 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.
不知何故,我找不到在${parameter#word}
和之间给出不同结果的情况${parameter##word}
。
我正在寻找能够说明两种语法之间不同行为的情况。顺便说一句,什么是最短匹配模式,什么是最长匹配模式?
这个例子可能更能说明问题
所以:
${parameter#pattern}
删除模式的最短匹配${parameter##pattern}
删除模式的最长匹配还有
${parameter%pattern}
和${parameter%pattern}
匹配字符串末尾的模式。我记得区别,因为之前#出现在(美国)键盘上。 %我刚刚找到了这个例子:
在这里,我们首先将字符串“00011”赋给变量'INT',然后我们输出以'INT'为参数,以'*0'为字型的任意一个
${parameter#word}
或进行参数扩展的结果。${parameter##word}
'*0' 与参数的最小 [prefix] 匹配只是 '0',而最大 [prefix] 匹配是 '000'。