Regular expression used for letters not failing for dots
#!/bin/bash filename=$1 while read first last email do #echo "$first" "$last" "$email" re='[a-zA-Z]+' if [[ "$first" =~ $re && "$last" =~ $re ]]; then echo "$email" else echo "bad" fi done < "$filename"
I am writing a bash script and trying to check the format list provided in the argument of the shell filename.File has the content as
Noble Able email@example.com Bob builder <firstname.lastname@example.org> Chris Charley email@example.com Doug_douglas@maingroup.east.com Ed Edwards firstname.lastname@example.org kola.coe.edu Kola Jones
As what I expect it should give error(print bad) at the last line kola.coe.edu since it has the dot character but my result is
email@example.com <firstname.lastname@example.org> email@example.com bad firstname.lastname@example.org Jones
Regular expressions in the =~ construct are not anchored (unlike patterns which might have confused you: [[ $first == +([a-zA-Z]) ]]).
Regex matches even if a substring of the string matches it. You need to use ^ and $ for the beginning and end of the string, respectively.
[[ $first =~ ^[a-zA-Z]+$ ]]