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

I'm trying to do a simple regex statement in a bash script that will match and substitute the end of a word. Ниже то, что я пытаюсь сделать.

wordh > word:’

Ниже приведен код, который я использую.

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

I'm not familiar with bash scripting and I'm thinking it has something to do with the $ because it's used to mark a variable. I've tried to escape it as well as adding another / после этого. Когда я удаляю $ it works (without checking the end of a word).

2 ответа

Решение

Регулярные выражения немного отличаются. Пытаться:

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

Со страницы руководства:

$ {Параметр / шаблон / строка}

.         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$/:’}
Другие вопросы по тегам