BASH – Determining that a variable is empty

Hi everyone

Today I had to script a bit inside a Linux VM on Azure. I had to make some test inside a bash script. To my extent I’am not a Linux expert, but I wanted to share a small tip I found.

How to find is a variable is empty or unset in BASH ?

To find if a variable is SET to empty to a “” string please use the comand :

if [ -z “$VAR” ];

Another way to write it would be with double bracket :

if [[ -z $var ]]
if [[ -z “$var” ]]
if [[ ! $var ]]
if [[ ! “$var” ]]

or that expression would check if it’s with a value :

if [[ $var ]]

 

Thanks everyone, enjoy the small tip

Advertisements
This entry was posted in linux. Bookmark the permalink.

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s