Using your regular expression and adapting it to bash
to test an address held in the variable email
:
#!/bin/bash
if [[ $email =~ ^[[:alnum:]_.-]+@[[:alnum:]_.-]+\.[[:alpha:]]{2,6}$ ]]
then
echo "Good address"
else
echo "Bad address"
fi
Notes:
this uses the
[[...]]
construct so an ordinary POSIX shell won't do:bash
or better is required.The
=~
operator matches a string, in this case the email address, to a POSIX Extended Regular Expression.The actual spec for email addresses, (have a look at RFC5322), is very complex (see this sample but outdated regex) and your regex is only an approximation.