Case insensitive comparison of strings in shell script

Viewed 252880

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

14 Answers

For zsh the syntax is slightly different, but still shorter than most answers here:

> str1='mAtCh'
> str2='MaTcH'
> [[ "$str1:u" = "$str2:u" ]] && echo 'Strings Match!'
Strings Match!
>

This will convert both strings to uppercase before the comparison.


Another method makes use zsh's globbing flags, which allows us to directly make use of case-insensitive matching by using the i glob flag:

setopt extendedglob
[[ $str1 = (#i)$str2 ]] && echo "Match success"

# this example compares the variable with a literal string 'match'
[[ $str1 = (#i)match ]] && echo "Match success"

I came across this great blog/tutorial/whatever about dealing with case sensitive pattern. The following three methods are explained in details with examples:

1. Convert pattern to lowercase using tr command

opt=$( tr '[:upper:]' '[:lower:]' <<<"$1" )
case $opt in
        sql)
                echo "Running mysql backup using mysqldump tool..."
                ;;
        sync)
                echo "Running backup using rsync tool..."
                ;;
        tar)
                echo "Running tape backup using tar tool..."
                ;;
        *)
                echo "Other options"
                ;;
esac

2. Use careful globbing with case patterns

opt=$1
case $opt in
        [Ss][Qq][Ll])
                echo "Running mysql backup using mysqldump tool..."
                ;;
        [Ss][Yy][Nn][Cc])
                echo "Running backup using rsync tool..."
                ;;
        [Tt][Aa][Rr])
                echo "Running tape backup using tar tool..."
                ;;
        *)
                echo "Other option"
                ;;
esac

3. Turn on nocasematch

opt=$1
shopt -s nocasematch
case $opt in
        sql)
                echo "Running mysql backup using mysqldump tool..."
                ;;
        sync)
                echo "Running backup using rsync tool..."
                ;;
        tar)
                echo "Running tape backup using tar tool..."
                ;;
        *)
                echo "Other option"
                ;;
esac

shopt -u nocasematch

The 'tr' utility (translate characters) is always present on all Unix/Linux machines, and is light-weight.

Here is a function that can be used to address case insensitive comparisons. Since the exact location of 'tr' can vary, we first probe its likely locations and store the correct location in an env var appropriately named "BIN_TR".

declare BIN_TR=$( ls /bin/tr /usr/bin/tr 2>/dev/null | head -1 );

That is then used in the function declaration.

toLowerCase() {
  echo "$*" | $BIN_TR '[:upper:]' '[:lower:]'
}

A solution using 'tr' is expected to be highly portable between different variations of OS and OS set-up. While 'awk' is also highly probable, the 'tr' utility is tiny compared to 'awk', and so a function using 'tr' is presumably lighter weight.

On Unix-like operating systems, the test command checks file types and compares values.

str1="MATCH"
str2="match"

if test $str1 =  $str2
  then
  echo "equal yes"
else
  echo "equal not"
fi

On Unix-like operating systems, the test command checks file types and compares values.

it's very simple that way.

in a few lines of code

Related