Bash 参数替换去掉第一个 # 字符
Bash parameter substitution strip first # character
有没有简单的方法 stripping/replacing #
字符从带有参数替换的 bash 变量中第一次出现?
我尝试了以下但没有用:
$ VERSION=0.11.3-issue#18.6a0b43d.123
$ echo ${VERSION#'#'}
$ echo ${VERSION#\#}
我希望我的输出是:
0.11.3-issue18.6a0b43d.123
# ^
# no #
有什么简单的解决办法吗?也许以完全不同的方式?
如果您只想 'delete' # 的第一次出现,请使用 ${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
有没有简单的方法 stripping/replacing #
字符从带有参数替换的 bash 变量中第一次出现?
我尝试了以下但没有用:
$ VERSION=0.11.3-issue#18.6a0b43d.123
$ echo ${VERSION#'#'}
$ echo ${VERSION#\#}
我希望我的输出是:
0.11.3-issue18.6a0b43d.123
# ^
# no #
有什么简单的解决办法吗?也许以完全不同的方式?
如果您只想 'delete' # 的第一次出现,请使用 ${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