Advertisement

Bash Scripting -z (bash shell script "if [ -z" )





Bash Shell scripting -z

If statement in if used with –z option checks either null is returned or not by the variable check. –z option specifies the zero length output or data. You could use –z option in bash script in the similar way you use any other option like –d, -f or –a.


Following example explains the use of –z option in bash script with If statement.  In the first statement the abc string is assigned the null value. Then if [ -z "$ abc " ] checks that $abc has anything or not as we assigned null. “abc is null” printed out.

 abc=''   # Zero-length ("null") string variable.

if [ -z "$ abc " ]
then
  echo "\$ abc is null."
else
  echo "\$ abc is NOT null."
fi     # $ abc is null.






0 comments: