Comparing Strings in ksh88

Hi I tried the following string comparison script in Ksh88

#!/bin/ksh
str1='aC'
str2='ABC'
if [ $str1==$str2 ]
 then
   echo "Equal"
else
   echo "Not Equal"
fi

Though str1 and str2 are not equal the script output says Equal .
Please correct me

Thanks

The if statement has to be modified as below as per syntax

if [ $str1 == $str2 ]

Without the spaces, it considers the expression as a string and evaluates to true as it is non-zero.

1 Like

Spaces on both side of "=" are necessary.

if [ $str1 == $str2 ]

I believe ksh88 uses single "=" syntax though not sure.

if [ $str1 = $str2 ]

Further, to avoid syntax error in case any variable is null, always quote the variables.

if [ "$str1" = "$str2" ]

Lastly, a safer way is,

if [ "x${str1}" = "x${str2}" ]
1 Like