bash tip of the day

After spending an hour or so staring at a regular expression in bash, I gave up and started looking at the interwebs… So what’s wrong on this?

random="aabbcc"
if [[  $random =~ "^a{1,2}b{1,2}c{1,2}$"  ]]; then
    echo "matching"
else
    echo "not matching"
fi

It’s supposed to compare $random with a regular expression (that’s what’s supposed to do the =~ operator). But no matter how hard I tried, it was always “not matching”

Fortunately this days we have stackoverflow with the answers.

 

 

 

 

(some spacing so that you can think on it….)

 

 

 

 

 

Short answer: no quotes around a regular expression!

I start getting a pattern here (punt intended), the clueless I’m usually while trying to find the solution, the easiest it is :)

Deixa un comentari

L'adreça electrònica no es publicarà Els camps necessaris estan marcats amb *

Podeu fer servir aquestes etiquetes i atributs HTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>