Search code examples
bashstringcase-insensitive

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?


Solution

  • 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%%@*}