DISQUS

DISQUS Hello! Tech-Recipes is using DISQUS, a powerful comment system, to manage its comments. Learn more.

Community Page

Tech-Recipes

Cookbook of Tech Tutorials
Jump to original thread »
Author

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

Started by qdideas · 9 months ago

No excerpt available. Jump to website »

7 comments

  • Wouldn't the following be better?

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

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

    Best Regards,
    Kibokina
  • 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
  • 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
  • if [ "$var" = "value" ]
    then
    echo not the same
    fi
  • These examples don't work.
  • 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.

Add New Comment

Returning? Login