Bourne/bash shell scripts: string comparison

Posted November 18, 2010 by Rex in 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

The Conversation

Follow the reactions below and share your own thoughts.

  • Anonymous

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

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

    Best Regards,
    Kibokina

    • Anonymous

      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.

  • bhargav

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

  • asd

    These examples don’t work.

  • Florin

    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.

  • BhupinderSingh is a layer

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

  • hema

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

    • Kousalya

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

    • Kousalya

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

      • Achu

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

        ( var value ‘one’)

        • Daman Sternpak

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

  • Smudge

    Wow, have you geeks ever thought about getting along?

    • Chris

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

  • Anonymous

    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.

  • Tolya_kashuba

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

    • Bermic2003

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

      $> [ “a” > “b” ]
      $> echo $?
      1
      $> [ “c” > “b” ]
      $> echo $?
      0

  • Man1

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

  • Ben D.,

    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.

  • Jan H. Reiche

    Ben D. is right,
    see man test for documentation

  • harry

    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….

  • Octofinger

    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.

  • http://killtube.org kung foo man

    After ] needs to be a ;