Regex 正则表达式匹配行尾$在Bash脚本中不起作用
我尝试在bash脚本中执行一个简单的regex语句,它将匹配并替换单词的结尾。下面是我想做的Regex 正则表达式匹配行尾$在Bash脚本中不起作用,regex,bash,eol,parameter-expansion,Regex,Bash,Eol,Parameter Expansion,我尝试在bash脚本中执行一个简单的regex语句,它将匹配并替换单词的结尾。下面是我想做的 wordh > word:’ 下面是我正在使用的代码 #!/bin/bash STAT=${STAT/h$/:’} 我不熟悉bash脚本,我认为它与$有关,因为它用于标记变量。我试着避开它,并在它之后添加另一个/。当我删除$时,它会工作(不检查单词的结尾)。regex的存在有些不同。 尝试: 从手册页: ${parameter/pattern/string} . The pat
wordh > word:’
下面是我正在使用的代码
#!/bin/bash
STAT=${STAT/h$/:’}
我不熟悉bash脚本,我认为它与
$
有关,因为它用于标记变量。我试着避开它,并在它之后添加另一个/
。当我删除$
时,它会工作(不检查单词的结尾)。regex的存在有些不同。
尝试:
从手册页:
${parameter/pattern/string}
. The pattern is expanded to produce a pattern just as in pathname
expansion. Parameter is expanded and the longest match of pat-
tern against its value is replaced with string. If Ipattern
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 beginning of the expanded value of
parameter. If pattern begins with %, it must match at the end
of the expanded value of parameter. If string is null, matches
of pattern are deleted and the / following pattern may be omit-
ted. If parameter is @ or *, the substitution operation is
applied to each positional parameter in turn, and the expansion
is the resultant list. If parameter is an array variable sub-
scripted with @ or *, the substitution operation is applied to
each member of the array in turn, and the expansion is the
resultant list.
$不是这个词的一部分 你可以试试
STAT=wordh\$
比尝试
STAT=${STAT/h$/:’}
更像是,它根本不是正则表达式。如果启用了extglob,它就是正则表达式,但默认情况下它不是。
STAT=${STAT/h$/:’}