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 :)

Una resposta a “bash tip of the day”

  1. Reading your article has greatly helped me, and I agree with you. But I still have some questions. Can you help me? I will pay attention to your answer. thank you.

Els comentaris estan tancats.