bashshellunix

how to implement null validation


set temp = "sunny45"

if [ - z $temp ]; then echo "empty" ; else echo "not empty" ; fi

The above code is giving below error when i tried this code on unix terminal

if: Expression syntax.

Please suggest should i source any thing in file and what should ne the file name of this code present.


Solution

  • Wipe spaces around equal sign and between - and z, then enclose variable in double-quotes:

    temp="sunny45"
    
    if [ -z "$temp" ]; then echo "empty" ; else echo "not empty" ; fi