0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Validate Email Address with Regex

Use a Bash script with a regex pattern like ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ inside an if [[ $email =~ regex ]]; then condition to validate an email address.
📋

Examples

Inputuser@example.com
OutputValid email address
Inputuser.name+tag@sub.domain.co
OutputValid email address
Inputinvalid-email@.com
OutputInvalid email address
🧠

How to Think About It

To validate an email in Bash, check if the input matches a pattern that includes allowed characters before and after the '@' symbol, and ends with a valid domain suffix. Use a regular expression to test this pattern inside an if statement.
📐

Algorithm

1
Get the email address input from the user or variable.
2
Define a regex pattern that matches valid email formats.
3
Check if the email matches the regex pattern.
4
If it matches, print 'Valid email address'.
5
Otherwise, print 'Invalid email address'.
💻

Code

bash
#!/bin/bash

email="$1"
regex='^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'

if [[ $email =~ $regex ]]; then
  echo "Valid email address"
else
  echo "Invalid email address"
fi
Output
Valid email address
🔍

Dry Run

Let's trace 'user@example.com' through the code

1

Input email

email='user@example.com'

2

Regex pattern

regex='^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'

3

Match check

'user@example.com' matches regex -> true

StepEmailRegex Match
1user@example.comtrue
💡

Why This Works

Step 1: Regex pattern meaning

The regex ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ensures the email starts with allowed characters, has an '@', then a domain and a valid suffix.

Step 2: Using Bash regex match

The [[ $email =~ $regex ]] syntax tests if the email matches the pattern, returning true or false.

Step 3: Conditional output

If the match is true, the script prints 'Valid email address'; otherwise, it prints 'Invalid email address'.

🔄

Alternative Approaches

Using grep with regex
bash
#!/bin/bash
email="$1"
if echo "$email" | grep -Eq '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'; then
  echo "Valid email address"
else
  echo "Invalid email address"
fi
Uses external grep command; slightly slower but compatible with older Bash versions.
Using Perl for validation
bash
#!/bin/bash
email="$1"
if perl -e 'exit($ARGV[0] =~ /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/ ? 0 : 1)' "$email"; then
  echo "Valid email address"
else
  echo "Invalid email address"
fi
More powerful regex support but requires Perl installed.

Complexity: O(n) time, O(1) space

Time Complexity

The regex match scans the email string once, so time complexity is linear in the length of the email.

Space Complexity

The script uses a fixed amount of memory for variables and regex, so space complexity is constant.

Which Approach is Fastest?

Bash's built-in regex matching is fastest; grep and Perl add overhead by calling external programs.

ApproachTimeSpaceBest For
Bash regex matchO(n)O(1)Simple scripts, fast execution
grep with regexO(n)O(1)Compatibility with older Bash versions
Perl regexO(n)O(1)Complex regex needs, powerful validation
💡
Always quote your variables in Bash to avoid word splitting and globbing issues.
⚠️
Beginners often forget to escape special characters in regex or omit quoting variables, causing errors.