Bash scripting, how to remove trailing substring, case insensitive?

I am modifying a script that reads in a user email. It is very simple, too simple.

echo -n "Please enter your example.com email address: "
read email
email=${email%%@example.com} # removes trailing @example.com from email
echo "email is $email"

This works, but only for lower case @example.com. How could I modify this to remove the trailing @example.com, case insensitive?

Answers


If you have bash 4:

email=${email,,}
email=${email%%@example.com}

Otherwise, perhaps just use tr:

email=$(echo "${email}" | tr "A-Z" "a-z")
email=${email%%@example.com}

Update:

If you are just wanting to strip the host (any host) then perhaps this is really what you want:

email=${email%%@*}

For Bash 3.2 and greater:

shopt -s nocasematch
email='JoHnDoE@eXaMpLe.CoM'
pattern='^(.*)@example.com$'
[[ $email =~ $pattern ]]
email=${BASH_REMATCH[1]}    # result: JoHnDoE

Need Your Help

.NET attributes in shipping assemblies fxcop

c# .net fxcop

I wonder if people (meaning the company/developers) really care about having [SuppressMessage] attributes lying around in the shipping assemblies.

C# continue in one line ForEach

c# foreach continue

Why I can't do that ? How to manipulate code ?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.