Linux shell/bash check if string is empty or not

To check if a string is empty/null or not use -n string or -z string with if condition.

-n string will return true if string is not empty/null.

-z string will return true if string is empty/null.

Example:

str="abc"

if [[ -n $str ]] ; then echo "not empyt/null"; fi

Since str is not empty, above command will return not empyt/null.

str=""

if [[ -z $str ]] ; then echo "empyt/null"; fi

Since str empty, above command will return empyt/null.

-Sany

3 comments on “Linux shell/bash check if string is empty or not

Leave a comment