1

I have Bash script below, trying to capture last digits of 'pingnet' but can not get a match. I verified in regex101 and my regex is correct:

pingnet="pingcount,site=DC,cur=200 total-up=988"
regex='(\d+)$'
if [[ $pingnet =~ $regex ]]
then
    echo "YES"
    echo "${BASH_REMATCH[1]}"
else
    echo "NOT"
    echo "${BASH_REMATCH[1]}"
fi

The result of running script is NOT.

slm
  • 363,520
  • 117
  • 767
  • 871
irom
  • 453
  • 2
  • 10
  • 20
  • 2
    `\d` doesn't work with bash's ERE, you need to use `[0-9]` – jesse_b Jul 19 '18 at 20:27
  • 2
    Maybe this will help: bash ERE regular expressions do not have the `\d` as explained in [Bash =~ regex and https://regex101.com/](https://unix.stackexchange.com/questions/421460/bash-regex-and-https-regex101-com) –  Jul 19 '18 at 20:32

1 Answers1

4

Bash's regex syntax does not recognize \d; use [[:digit:]] instead:

pingnet="pingcount,site=DC,cur=200 total-up=988"
regex='([[:digit:]]+)$'
Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250