Using regex in a bash script

I want to verify if the string has the pattern DEV-x. Where x could be any number.

commit_msg="DEV-234"

if [[ "$commit_msg" =~ .*[DEV-][0-9]{4}.* ]]; then
        echo "inside if";
else
        echo "else block";
fi

I tried the above, but it does not work and the flow always passes through the else block. What is it that I am missing?

2 answers

  • answered 2020-09-28 06:00 Christian Baumann

    You are looking for DEV-[0-9]+

  • answered 2020-09-28 06:22 User123

    If you're only looking for the pattern : DEV-x inside a string, where x could be any number, then below should work:

    if [[ "$commit_msg" =~ DEV-[0-9]+ ]];