2

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.

2
  • 2
    \d doesn't work with bash's ERE, you need to use [0-9] Commented Jul 19, 2018 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/ Commented Jul 19, 2018 at 20:32

1 Answer 1

5

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

pingnet="pingcount,site=DC,cur=200 total-up=988"
regex='([[:digit:]]+)$'

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.