DISQUS

Tech-Recipes: Bourne/bash shell scripts: string comparison | Bourne shell scripting | Tech-Recipes

  • Anonymous · 5 years ago
    Wouldn't the following be better?

    if [ -z $var ];then
    echo null
    fi
  • Anonymous · 1 year ago
    i believe the $var needs to be in quotes, like so:

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

    Best Regards,
    Kibokina
  • netuddki · 2 weeks ago
    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.
  • guest · 5 years ago
    the following syntax is wrong

    if [ $var == "" ] ......

    because if the variable $var is empty the test is
    if [ == "" ] and gives an error

    you are obliged to quote the var :
    if [ "$var" == "" ] ...


    Sergio
  • BhupinderSingh · 4 years ago
    this can also be used to compare 2 strings

    s1 = "as"
    s2 = "bs"

    if test $s1 == $s2
    then
    echo s1 and s2 are equal
    fi
  • bhargav · 7 months ago
    if [ "$var" = "value" ]
    then
    echo not the same
    fi
  • asd · 7 months ago
    These examples don't work.
  • Florin · 6 months ago
    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.