如何有条件地提取bash字符串的一部分.

Ale*_*lex 0 bash

我有一个看起来像的bash字符串

TEST="tags/1.2.3-abc"
Run Code Online (Sandbox Code Playgroud)

我希望在斜杠后提取部分,即"1.2.3-abc",当且仅当字符串以短语"tags /"开头时.如果后者不是这种情况,我希望字符串不变.

例子:

Input: tags/1.2.3-abc  Output: 1.2.3-abc
Input: 1.2.3-abc       Output: 1.2.3-abc
Input: trunk           Output: trunk 
Run Code Online (Sandbox Code Playgroud)

Jon*_*Lee 5

听起来像是Bash中替代运营商的工作.

尝试类似的东西:

TEST="tags/1.2.3-abc"
echo ${TEST#tags/}
Run Code Online (Sandbox Code Playgroud)

您可以在此处阅读有关此语法的更多信息:http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion