04 Jan 2012 @ 8:00 PM 

Hello, and thank you for coming again !

I have noticed that some of you are coming via the RSS feed, and it is nice to see you are following this blog thoroughly !

Today, the topic is ” Equal Tilde operator”

The situation

You have a variable, and you need to check if the content is a single digit, or the letter “a” or the letter “A” or the letter “b” or the letter “B”

The solution

You do not use the equal tilde and you die of boredom :

if [[ ${var} = [0-9] ]] || [ “$var” = “a” ] || [ “$var” = “A” ] || [ “$var” = “b” ] || [ “$var” = “B” ] ; then

or you use the equal tile operator :

if [[ “${var}” =~ \^\[0-9]\|a\|b\|A\|B\$ ]] ; then

and you can live another day.

The equal tilde operator allows you to use regex (the same ones you used for see, remember ?) in an if command.

Note the two square brackets.

Thank you for reading, and see you tomorrow !

 

Nota bene :

You should not put the right side of the =~ operator in quotes, as this will mean a string, and not a regex.

If you do not want escaping everything that might need to be escaping, just put your complicated regex in a variable :

RegEx=”My Complicated Regex”

if [[ “${var}” =~ ${RegEx} ]]; then

 

Posted By: Dimi
Last Edit: 05 Jan 2012 @ 07:54 PM

EmailPermalink
Tags


 

Responses to this post » (None)

 
Post a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

 


 Last 50 Posts
Change Theme...
  • Users » 66
  • Posts/Pages » 25
  • Comments » 4
Change Theme...
  • VoidVoid « Default
  • LifeLife
  • EarthEarth
  • WindWind
  • WaterWater
  • FireFire
  • LightLight