Added section for =~ operator

This commit is contained in:
Martin Nicholson 2018-01-16 16:03:13 +00:00
parent 462f94b967
commit 8d2055483e

View File

@ -161,6 +161,16 @@ then
echo "This will run if $Name is Daniya OR Zach." echo "This will run if $Name is Daniya OR Zach."
fi fi
# There is also the =~ operator, which tests a string against a Regex pattern:
Email=me@example.com
if [[ "$Email" =~ [a-z]+@[a-z]{2,}\.(com|net|org) ]]
then
echo "Valid email!"
fi
# Note that =~ only works within double [[ ]] square brackets,
# which are subtly different from single [ ].
# See the appropriate [manual section](http://www.gnu.org/software/bash/manual/bashref.html#Conditional-Constructs) for more on this.
# Expressions are denoted with the following format: # Expressions are denoted with the following format:
echo $(( 10 + 5 )) # => 15 echo $(( 10 + 5 )) # => 15