正则表达式匹配行尾 $ 在 Bash 脚本中不起作用

Regex matching end of a line $ not working in Bash Script

我正在尝试在 bash 脚本中做一个简单的正则表达式语句来匹配和替换单词的结尾。以下是我正在尝试做的事情。

wordh > word:’

下面是我正在使用的代码。

#!/bin/bash
STAT=${STAT/h$/:’}

我不熟悉 bash 脚本,我认为它与 $ 有关,因为它用于标记变量。我试图逃避它并在它之后添加另一个 / 。当我删除 $ 时它起作用了(不检查单词的结尾)。

那里的正则表达式有点不同。 尝试:

STAT=${STAT/%h/:’}

来自手册页:

${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$/:’}