有没有简单的方法从参数替换的bash变量中剥离/替换第一次出现的#
字符?
我尝试了以下但是没有用:
$ VERSION=0.11.3-issue#18.6a0b43d.123
$ echo ${VERSION#'#'}
$ echo ${VERSION#\#}
我希望我的输出为:
0.11.3-issue18.6a0b43d.123
# ^
# no #
任何简单的解决方案?也许是以完全不同的方式?
答案 0 :(得分:5)
如果您只想“删除”第一次出现的#,请使用${parameter/pattern}
。
${parameter/pattern/string}
Pattern substitution. The pattern is expanded to produce a pat-
tern just as in pathname expansion. Parameter is expanded and
the longest match of pattern against its value is replaced with
string. If pattern begins with /, all matches of pattern are
replaced with string. Normally only the first match is
replaced. If pattern begins with #, it must match at the begin-
ning of the expanded value of parameter.
?
和*
)。#
具有特殊含义,这就是我们将其替换为\
的原因。然后,序列\#
匹配文字#
,而在模式的开头没有#
的特殊含义。 示例强>
VERSION=0.11.3-issue#18.6a0b43d.123
echo ${VERSION/\#/}
<强>输出强>
0.11.3-issue18.6a0b43d.123