Bash regex matching not working in 4.1

7,869

Check this answer on SO. Since you are using 3.00 version of bash 3, it might regard your problem. Shortly, starting from 3.2 version, quoting the string argument to the [[ command's =~ operator forces string matching, so the correct pattern for bash 4 should be:

$ [[ "20110228" =~ ^[0-9]{8}$ ]] && echo matches
matches
Share:
7,869

Related videos on Youtube

CurtainDog
Author by

CurtainDog

Programmer

Updated on September 17, 2022

Comments

  • CurtainDog
    CurtainDog almost 2 years

    Upgraded to Bash4 and found that it is not matching regexes:

    $ echo $BASH_VERSION
    4.1.2(1)-release
    
    $ [[ "20110228" =~ "^[0-9]{8}$" ]] && echo matches
    

    But Bash 3.0 is:

    $ echo $BASH_VERSION
    3.00.16(1)-release
    
    $ [[ "20110228" =~ "^[0-9]{8}$" ]] && echo matches
    matches
    

    Why might this be? Have I not installed it correctly?