Bourne/bash shell scripts: string comparison

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

The Conversation

Follow the reactions below and share your own thoughts.

22 Responses to “Bourne/bash shell scripts: string comparison”

  1. October 22, 2008 at 3:38 am, Anonymous said:

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

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

    Best Regards,
    Kibokina

    Reply

    • October 25, 2009 at 11:59 pm, Anonymous said:

      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.

      Reply

  2. March 27, 2009 at 5:15 pm, bhargav said:

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

    Reply

  3. April 02, 2009 at 8:58 am, asd said:

    These examples don’t work.

    Reply

  4. April 16, 2009 at 12:35 pm, Florin said:

    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.

    Reply

  5. November 18, 2009 at 7:20 pm, BhupinderSingh is a layer said:

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

    Reply

  6. April 21, 2010 at 12:40 pm, hema said:

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

    Reply

    • May 17, 2010 at 9:42 am, Kousalya said:

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

      Reply

    • May 17, 2010 at 9:51 am, Kousalya said:

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

      Reply

      • June 07, 2010 at 9:57 am, Achu said:

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

        ( var value ‘one’)

        Reply

        • May 06, 2011 at 5:53 pm, Daman Sternpak said:

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

          Reply

  7. September 21, 2010 at 7:46 pm, Smudge said:

    Wow, have you geeks ever thought about getting along?

    Reply

    • June 25, 2012 at 9:49 pm, Chris said:

      > Good call. The left hemisphere of the brain is a loveless thing! Finds a wealth in division…

      Reply

  8. November 18, 2010 at 7:27 pm, Anonymous said:

    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.

    Reply

  9. January 16, 2011 at 3:46 am, Tolya_kashuba said:

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

    Reply

    • February 21, 2011 at 2:50 pm, Bermic2003 said:

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

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

      Reply

  10. February 23, 2011 at 4:48 pm, Man1 said:

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

    Reply

  11. October 11, 2011 at 3:56 pm, Ben D., said:

    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.

    Reply

  12. June 13, 2012 at 6:50 pm, Jan H. Reiche said:

    Ben D. is right,
    see man test for documentation

    Reply

  13. June 18, 2012 at 12:39 pm, harry said:

    Usually searching the web is pretty useful. Except in this case. Incredible. If you are looking for the real answer, type: man test
    Some more tips:
    * Only use a single = sign
    * make sure you have spaces around the = sign
    * You do not need to put quotation marks around the variable names if you are comparing strings
    Pfff….

    Reply

  14. September 07, 2012 at 11:12 am, Octofinger said:

    This is ok in bash:
    [ $var == "hello" ]

    But in Bourne, you can’t use two = signs, thus it has to be written:
    [ $var = "hello" ]

    I believe single = works in bash too, but it’s very poor that the article writer never bother to check these things up, and even worse that the article isn’t updated.

    Reply

  15. March 01, 2013 at 4:06 pm, kung foo man said:

    After ] needs to be a ;

    Reply

Leave a Reply