SreeVik
SreeVik

Reputation: 73

Special characters within an If condition

I am trying to write an if statement which checks if the first two characters of particular variable starts with '$$'. I have tried the following but the if condition is always evaluating to true.

var1=$$abc    
var2=$$def

var1_cut=`echo $var1 | cut -c -2`    
var2_cut=`echo $var2 | cut -c -2`

if [[ $var1_cut == "$$" && $var2_cut== "$$" ]]    
then    
  break
else    
  echo $var1  
fi

Could anyone help me out here. Is there anything to watch out for while trying to match $$ in if statements

Upvotes: 0

Views: 435

Answers (1)

P.P
P.P

Reputation: 121407

Use single quotes to avoid expansion and double while comparing.

If you are using bash, you don't need to use cut. You can do that in bash itself.

var1='$$abc'
var2='$$def'

var1_cut=${var1:0:2}
var2_cut=${var2:0:2}

if [[ "$var1_cut" == '$$' && "$var2_cut" == '$$' ]]
then
  # Do something
else
  echo "$var1"
fi

Upvotes: 1

Related Questions