Case insensitive comparison of strings in shell script Case insensitive comparison of strings in shell script shell shell

Case insensitive comparison of strings in shell script


In Bash, you can use parameter expansion to modify a string to all lower-/upper-case:

var1=TesTvar2=tEstecho ${var1,,} ${var2,,}echo ${var1^^} ${var2^^}


All of these answers ignore the easiest and quickest way to do this (as long as you have Bash 4):

if [ "${var1,,}" = "${var2,,}" ]; then  echo ":)"fi

All you're doing there is converting both strings to lowercase and comparing the results.


if you have bash

str1="MATCH"str2="match"shopt -s nocasematchcase "$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"  }}'