views:

469

answers:

4

The == is used to compare two string in shell script, however I want to compare two strings by ignoring case, how it can be done.Do we any standard command for this.

A: 

shopt -s nocaseglob

ennuikiller
No manual entry for shopt.Its not available on my system
Sachin Chourasiya
nocasematch for strings.
ghostdog74
@ghostdog, I like the tone
Sachin Chourasiya
A: 

One way would be to convert both strings to upper or lower:

test $(echo "string" | /bin/tr -s '[:upper:]' '[:lower:]') = $(echo "String" | /bin/tr -s '[:upper:]' '[:lower:]') && echo same || echo different

Another way would be to use grep:

echo "string" | grep -qi '^String$' && echo same || echo different
Randy Proctor
A: 

if you have bash

str1="MATCH"
str2="match"
shopt -s nocasematch
case "$str1" in
 $str2 ) echo "match";;
 *) echo "no match";;
esac

otherwise, you should tell us what shell you are using.

alternative, using awk

str1="MATCH"
str2="match"
awk -vs1="$str1" -vs2="$str2" 'BEGIN {
  if ( tolower(s1) == tolower(s2) ){
    print "match"
  }
}'
ghostdog74
A: 
var1=match 
var2=MATCH 
if echo $var1 | grep -i "^${var2}$" > /dev/null ; then
  echo "MATCH"
fi
Larry