Sachin Chourasiya Sachin Chourasiya - 6 months ago 61
Bash Question

Case Insensitive comparision of strings in Shell script

The

==
operator is used to compare two string in shell script, however I want to compare two strings ignoring case, how it can be done? Is there any standard command for this?

Answer

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"
  }
}'
Comments