Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. Below is what I'm trying to do.

wordh > word:’

Below is the code I'm using.

#!/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 / after it. When I remove the $ it works (without checking the end of a word).

like image 917
user2743 Avatar asked Jan 09 '16 06:01

user2743


1 Answers

The regex's there are a little different. Try:

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

From the man page:

${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.
like image 142
John Hascall Avatar answered Oct 20 '22 01:10

John Hascall