Compare a string using sh shell

ShellUnixSh

Shell Problem Overview


I am using SH shell and I am trying to compare a string with a variable's value but the if condition is always execute to true. Why?

Here is some code:

Sourcesystem="ABC"

if [ "$Sourcesystem" -eq 'XYZ' ]; then 
    echo "Sourcesystem Matched" 
else
    echo "Sourcesystem is NOT Matched $Sourcesystem"  
fi;

echo Sourcesystem Value is  $Sourcesystem ;

Even this is not working:

Sourcesystem="ABC"

if [ 'XYZ' -eq "$Sourcesystem" ]; then 
    echo "Sourcesystem Matched" 
else
    echo "Sourcesystem is NOT Matched $Sourcesystem"  
fi;

echo Sourcesystem Value is  $Sourcesystem ;

Secondly, can we match this with a NULL or empty string?

Shell Solutions


Solution 1 - Shell

You should use the = operator for string comparison:

Sourcesystem="ABC"

if [ "$Sourcesystem" = "XYZ" ]; then 
    echo "Sourcesystem Matched" 
else
    echo "Sourcesystem is NOT Matched $Sourcesystem"  
fi;

man test says that you use -z to match for empty strings.

Solution 2 - Shell

-eq is used to compare integers. Use = instead.

Solution 3 - Shell

eq is used to compare integers use equal '=' instead , example:

if [ 'AAA' = 'ABC' ];
then 
    echo "the same" 
else 
    echo "not the same"
fi

good luck

Solution 4 - Shell

I had this same problem, do this

if [ 'xyz' = 'abc' ]; then
  echo "match"
fi

Notice the whitespace. It is important that you use a whitespace in this case after and before the = sign.

Check out "Other Comparison Operators".

Solution 5 - Shell

-eq is the shell comparison operator for comparing integers. For comparing strings you need to use =.

Solution 6 - Shell

-eq is a mathematical comparison operator. I've never used it for string comparison, relying on == and != for compares.

if [ 'XYZ' == 'ABC' ]; then   # Double equal to will work in Linux but not on HPUX boxes it should be if [ 'XYZ' = 'ABC' ] which will work on both
  echo "Match"
else
  echo "No Match"
fi

Solution 7 - Shell

Of the 4 shells that I've tested, ABC -eq XYZ evaluates to true in the test builtin for zsh and ksh. The expression evaluates to false under /usr/bin/test and the builtins for dash and bash. In ksh and zsh, the strings are converted to numerical values and are equal since they are both 0. IMO, the behavior of the builtins for ksh and zsh is incorrect, but the spec for test is ambiguous on this.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJames BondView Question on Stackoverflow
Solution 1 - ShellMithrandirView Answer on Stackoverflow
Solution 2 - ShellPiotr PraszmoView Answer on Stackoverflow
Solution 3 - ShellQign20View Answer on Stackoverflow
Solution 4 - ShellEswar YagantiView Answer on Stackoverflow
Solution 5 - ShellcodaddictView Answer on Stackoverflow
Solution 6 - ShellJasonView Answer on Stackoverflow
Solution 7 - ShellWilliam PursellView Answer on Stackoverflow