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
  • 369,824
irom
  • 483
  • 2
  • 10
  • 20

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
  • 67,283
  • 35
  • 116
  • 255