Bash regex help

I've been using the following regex below in a bash script on RHEL 5.5 using version
GNU bash, version 3.2.25(1)-release

I've tried using the script on RHEL 6.3 which uses GNU bash, version 4.1.2(1)-release

I assume there's been alot of changes to bash since that's quite a jump in revisions. I'm wondering if anyone can help me sort what might have to be modified in the code to make this work again.

If I enter an e-mail address in a simple format like:

user.user@test.com the scripts sees this as an invalid e-mail now, whereas before this would pass the regex.

#!/bin/bash
echo Enter the users e-mail address.

read ADDRESS

if [[ "$ADDRESS" =~ "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$" ]] ; then
   echo $?
   echo Proceeding!!
else
   echo $?
   echo You entered an invalid e-mail address!!!
exit 1
fi

There should not be double quotes around the regex...

1 Like

So simple..Thanks

You also shouldn't depend on the meaning of $?, after the test (if [ ...]).

$ cat SomeScript
ls sdadadsasdasd
echo $?

ls sdadadsasdasd

if [ 1 -eq 1 ]; then
  echo $?
  echo Yes, 1 is 1
else
  echo $?
  echo No, not 1 is not 1
fi


$ ./SomeScript
ls: sdadadsasdasd: No such file or directory
1
ls: sdadadsasdasd: No such file or directory
0
Yes, 1 is 1

Thanks, however I wasn't relying on the exit status, I just put that in the script temporarily while trying to test.

I know, but just thought it worth mentioning :slight_smile:

I'm having the same issue in this regex. if I remove the quotes in this one then
something like Test Name comes back as invalid whereas in bash 3.2 Test Name would be valid but test name would be invalid. The idea is the first and last name need to start with an upper case letter of course.

if [[ "$givenName" =~ "^[A-Z]([a-z]|'[A-Za-z])+\s+[A-Z]([a-z]|'[A-Za-z])+(-[A-Z]([a-z]|'[A-Za-z])+)*$" ]]
  then
    echo -e "${bldgrn}$givenName is a valid name.${txtrst}"
else
    echo -e "${bldred}$givenName is an invalid name!!${txtrst}"
exit 1
fi

At some point between version 3 and version 4, bash began to treat a quoted right-hand operand of =~ as a string instead of a pattern. This explains why it used to work one way and now it works another.

Regards,
Alister

If I use the code below without the double and single quotes Test Name is invalid.

if [[ "$givenName" =~ ^[A-Z]([a-z]|[A-Za-z])+\s+[A-Z]([a-z]|[A-Za-z])+(-[A-Z]([a-z]|[A-Za-z])+)*$ ]]
  then
    echo -e "${bldgrn}$givenName is a valid name.${txtrst}"
else
    echo -e "${bldred}$givenName is an invalid name!!${txtrst}"
exit 1
fi

Try replacing \s with [[:space:]]

1 Like

This is perfect. It actually works better that it did before because now a hyphenated last name is now valid as well.

Thank you.

I guess I'm going to have to do some more reading on regular expressions so I can understand them better. Do you have any recommendations on a resource?

From Bash CHANGELOG:

1 Like

I wasn't aware of that, fpmurphy. Thanks for pointing it out. What I was remembering was:

Regards,
Alister