How to put the domain of a email address into a variable in BASH

I am new to BASH scripting and I wanted to ask, how would I put the domain of a email address into a variable. For example:

email="accounts.google@google.com"
email_domain= the domain of $email in this example "google.com"

I already know the regex to match the domain, which would be (?<=@)[^.]+(?=\.).* (https://regex101.com/r/eZ2iEO/1) but I do not know what else to do, how do I get the domain?

>Solution :

The simple solution is email_domain=${email##*@} which removes everything before last @ including @.

It is possible that this is not perfectly correct parsing according to RFC for every email address possible and it does not validate email address nor domain format, but emails according to RFC can be very wild.

Leave a Reply