http://wiki.bash-hackers.org/syntax/ccmd/conditional_expression
Regular Expression Matching
Using the operator =~
, the left hand side operand is matched against the extended regular expression (ERE) on the right hand side.
This is consistent with matching against patterns: Every quoted part of the regular expression is taken literally, even if it contains regular expression special characters.
Best practise is to put the regular expression to match against into a variable. This is to avoid shell parsing errors on otherwise valid regular expressions.
REGEX="^[[:upper:]]{2}[[:lower:]]*$" # Test 1 STRING=Hello if [[ $STRING =~ $REGEX ]]; then echo "Match." else echo "No match." fi # ==> "No match."