Bourne/bash shell scripts: string comparison

Contributor Icon Contributed by Rex  
Tag Icon Tagged: Bourne shell scripting  

Brief tutorial describing how to do string comparisons. Recently updated thanks to comments from our users.

Our original tutorial needed correcting for the case if $var is null. We have updated our original article.

To determine if the value of a variable ($var) is empty (null):

if [ "$var" == "" ]
then
echo variable is null
fi

To determine if the value of a variable is not empty:

if [ "$var" != "" ]
then
echo variable is not null
fi

To compare the contents of a variable to a fixed string:

if [ "$var" == "value" ]
then
echo is the same
fi

To determine if variable’s contents are not equal to a fixed string:

if [ "$var" != "value" ]
then
echo not the same
fi

 

17 Comments -


  1. Anonymous said on October 22, 2008

    i believe the $var needs to be in quotes, like so:

    if [ -z "$var" ];then
    echo null
    fi

    Best Regards,
    Kibokina

  2. bhargav said on March 27, 2009

    if [ "$var" = "value" ]
    then
    echo not the same
    fi

  3. asd said on April 2, 2009

    These examples don’t work.

  4. Florin said on April 16, 2009

    This is the method i curently to test if a string is empty:

    if [ "X${VAR}" = "X" ]; then
    echo “Empty string”
    fi

    Testing for equality follows the same logic but without X.

  5. Anonymous said on October 25, 2009

    The correct solution is here a year ago. It should be corrected in the article too, so one won’t try those for 10 minutes before finding the answer here.

  6. BhupinderSingh is a layer said on November 18, 2009

    You’re lying. That dosen’t work.
    test: 40: ==: unexpected operator

  7. hema said on April 21, 2010

    Can a variable be compared with an another variable eg : if [[ $var1 == $var2 ]]

  8. Kousalya said on May 17, 2010

    you can compare two string variables using
    if [ $var1 == $var2 ]

  9. Kousalya said on May 17, 2010

    you can compare two string variables using
    if [ $var1 == $var2 ]
    if [ $var1 = $var2 ]

  10. Achu said on June 7, 2010

    both the commands are not working, it throwing an error
    ./hello: [one=one]: not found

    ( var value ‘one’)

  11. Smudge said on September 21, 2010

    Wow, have you geeks ever thought about getting along?

  12. Anonymous said on November 18, 2010

    Thanks for the comments. Somehow this article fell off our radar and I didn’t realize we had an issue with it. I really appreciate Martin for dropping me the email about the issue. The original tutorial has been updated.

  13. Tolya_kashuba said on January 16, 2011

    How can I compare two strings on less or greater? signs > and

  14. Bermic2003 said on February 21, 2011

    I do this by escaping less and greater sign, and it seems to work fine :

    $> [ "a" > "b" ]
    $> echo $?
    1
    $> [ "c" > "b" ]
    $> echo $?
    0

  15. Man1 said on February 23, 2011

    == never works. bloody are you kidding by posting wrong things over here?

  16. Daman Sternpak said on May 6, 2011

    I had the same exact problem and it drove me nutty batshiat-insane. The problem: you need a space before and after the = sign.

  17. Ben D., said on October 11, 2011

    A number of commenters have hinted at this but no one has expressed it with perfect clarity, so I’ll give it a whirl:

    “==” is not a valid operator in Bourne Shell.

    The string comparison operator is “=”.

    Thank you, that is all.

 

RSS feed for comments on this post. TrackBack URL

Leave a comment -