Bash regular expressions in RHEL 6 - TAM tip

Posted on

Quoted regular expressions no longer work in '[[ ]]' in Red hat Enterprise Linux 6. The way to do this now is to assign the regex pattern to a variable and then use the variable in a test.

 

Example
 

 

This will work in Bash 3 but not in Bash 4 because Bash 4 considers 'Link encap:Ethernet HWaddr (.*)' as a string instead of Regular expression:

 

 

if ![[ $info =~ 'Link encap:Ethernet HWaddr (.*)' ]]
 

 

To make the expression work in Bash 4 do the following:

 

info2='Link encap:Ethernet HWaddr (.*)'
if ![[ $info =~ $info2 ]]

Responses