I am writing a simple bash script where user input the domain name (example.com) and it greps the email address from whois command output.

I want only grep the email with the same input domain ([email protected]).

Below command works if I write the domain (example.com) but does not work with $domain. I do not know how to use the $domain variable within the grep function. Any help is very much appreciated, thank you.

whois $domain | grep -m1 -EiEio '\b[A-Z0-9._%+-]+@[$domain]+\.[A-Z]{2,4}\b'

1 Answer 1


Variables are not expanded within single quotes. In this case, simply switching to double quotes would probably be enough. Also, remove the [ and ] around $domain. With the square brackets, you would create a bracket expression that would match any single character in the domain name (but not the actual domain name itself).

In the end, you would end up with something like

whois "$domain" |
grep -m1 -Eio "\b[[:alnum:]._%+-]+@$domain\b"

I deleted the \.[A-Z]{2,4} bit at the end of your expression because I didn't actually see any use with it, and I used a symbolic name for matching alphanumeric before the @ sign.

An additional enhancement would be to replace any dot in $domain with \. before using it in the pattern:

whois "$domain" |
grep -m1 -Eio "\b[[:alnum:]._%+-]+@${domain//./\\.}\b"

This requires bash or any other shell that understands the non-standard ${variable//pattern/word} parameter substitution.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .