$ 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