$ a="artifact-1.2.3.zip"; a="${a#*-}"; echo "${a%.*}"
“#
模式”删除模式,只要它符合开始的$a
。 模式的语法与文件名匹配中使用的语法相似。 在我们的例子中,
*
是任何字符序列。
-
表示字面短划线。
- 因此
#*-
匹配所有内容,包括第一个短划线。
- 因此
${a#*-}
扩展到任何$a
将扩大至, 除了artifact-
从扩张取出, 留给我们1.2.3.zip
。
类似地,“%
图案”删除图案,只要它的膨胀端匹配。 在我们的案例中,
.
字面点。
*
任何字符序列。
- 因此
%.*
就是一切,包括最后的点直到字符串的末尾。
- 因此,如果
$a
扩大到1.2.3.zip
, 然后${a%.*}
扩大到1.2.3
。
工作完成。
本手册页内容如下(至少在我的机器,情况因人而异上):
${parameter#word}
${parameter##word}
The word is expanded to produce a pattern just as in pathname
expansion. 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}
The word is expanded to produce a pattern just as in pathname
expansion. If the pattern matches a trailing portion of the
expanded value of parameter, then the result of the expansion is
the expanded value of parameter with the shortest matching pat-
tern (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.
HTH!
编辑
荣誉对@ x4d了详细的解答。 尽管如此,仍然认为人们应该RTFM。 如果他们不明白手册, 然后张贴另一个问题。
好又简单,谢谢! – carlspring 2013-04-22 17:54:35
这里列出的任何解释或相关文档会更好。 – Judking 2015-05-13 08:52:13
RTFM。公平的说,手册页非常大。在该文件中查找两个连续的哈希,并且您将位于右侧区域(_less_中的'/ ##'命令) – bobbogo 2015-05-13 09:50:19