Possible Duplicate:
How do I use regular expressions in bash scripts?
Why isn't this working?
if [[ "foo" =~ "[f][o][o]" || "foo" =~ "(foo)" || "foo" =~ ".*" ]]
then
echo "Success"
else
echo "Fail"
fi
# Result: Fail
# Expected: Success
if [[ "foo" =~ "foo" ]]
then
echo "Success"
else
echo "Fail"
fi
# Result: Success
# Expected: Success
Remove the quotes from each regex:
if [[ "foo" =~ [f][o][o] || "foo" =~ (foo) || "foo" =~ .* ]]
then
echo "Success"
else
echo "Fail"
fi
Apparently, the quotes force the regex to be interpreted as a literal string.
精彩评论