bash regex doesn’t match dots if regex is a string?

This question already has an answer here:

I attempted to do a bash regex with an inline string, like if [[ $var =~ "...abc..." ]] but the dot operator doesn’t seem to work? It does work as expected if I assign it as a variable first, here’s an example:

regex="...abc..."
var="123abc123"

if [[ "$var" =~ $regex ]]
then
    echo "matches when used as var" # it gets here
else
    echo "doesn't match!"
fi

if [[ "$var" =~ "$regex" ]]
then
    echo "matches"
else
    echo "doesn't match when used as string?" # it gets here
fi

Same result if I use =~ "...abc..."

It appears to only match the “period character” not the regex style match any char style…

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.

Roger's meanderings, notes to himself, bug reports, and other things