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

Advertisements

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s