0

I have a value in a variable that may be absolute or relative url, and I need to check which one it is.

I have found that there's a =~ operator in [[, but I can't get it to work. What am I doing wrong?

url="http://test"
if [[ "$url" =~ "^http://" ]];
    then echo "absolute.";
fi;
3
  • Recommended reading on Regular Expressions in Bash Commented Feb 18, 2015 at 10:26
  • Regarding your latest deleted question. As you deleted it, I was typing an answer based on one of my previous answers; perhaps it will help Commented Feb 22, 2015 at 13:11
  • @MikePennington I just wanted to avoid any more downvotes, after I found out that global is not needed there at all, it was all clear. Commented Feb 22, 2015 at 13:15

1 Answer 1

2

You need to use regex without quote:

url="http://test"
if [[ "$url" =~ ^http:// ]]; then
    echo "absolute."
fi

This outputs `absolute. as regex needs to be without quote in newer BASH (after BASH v3.1)

Or avoid regex and use glob matching:

if [[ "$url" == "http://"* ]]; then
    echo "absolute."
fi
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.