Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading