Blog

03/06/2019

Bash shell find out if a variable has NULL value OR not



I am a new to shell scripting. How do I check for NULL value in a Linux or Unix shell scripts?

kbd { display: inline-block; margin: 0 .1em; padding: .1em .6em; font-family: Arial,”Helvetica Neue”,Helvetica,sans-serif; font-size: 11px; line-height: 1.4; color: #242729; text-shadow: 0 1px 0 #FFF; background-color: #e1e3e5; border: 1px solid #adb3b9; border-radius: 3px; box-shadow: 0 1px 0 rgba(12,13,14,0.2), 0 0 0 2px #FFF inset; white-space: nowrap;}

You can quickly test for null or empty variables in a Bash shell script. You need to pass the -z or -n option to the test command or to the if command or use conditional expression. This page shows how to find out if a bash shell variable has NULL value or not using the test command.

To find out if a bash variable is null:

  1. Return true if a bash variable is unset or set to the null (empty) string: if [ -z "$var" ]; then echo "NULL"; else echo "Not NULL"; fi
  2. Another option to find if bash variable set to NULL: [ -z "$var" ] && echo "NULL"
  3. Determine if a bash variable is NULL: [[ ! -z "$var" ]] && echo "Not NULL" || echo "NULL"

Bash shell find out if a variable has NULL value OR not

Let us see test command syntax and examples in details. The syntax is as follows for the if command:

my_var="nixCraft"
if [ -z "$my_var" ]
then
      echo "$my_var is NULL"
else
      echo "$my_var is NOT NULL"
fi

my_var="nixCraft" if [ -z "$my_var" ] then echo "$my_var is NULL" else echo "$my_var is NOT NULL" fi

OR

my_var=""
if test -z "$my_var" 
then
      echo "$my_var is NULL"
else
      echo "$my_var is NOT NULL"
fi

my_var="" if test -z "$my_var" then echo "$my_var is NULL" else echo "$my_var is NOT NULL" fi

Another option to check if bash shell variable is NULL or not

[ -z "$my_var" ] && echo "NULL"
[ -z "$my_var" ] && echo "NULL" || echo "Not NULL"

[ -z "$my_var" ] && echo "NULL" [ -z "$my_var" ] && echo "NULL" || echo "Not NULL"

OR

[[ -z "$my_var" ]] && echo "NULL"
[[ -z "$my_var" ]] && echo "NULL" || echo "Not NULL"

[[ -z "$my_var" ]] && echo "NULL" [[ -z "$my_var" ]] && echo "NULL" || echo "Not NULL"

OR

## Check if $my_var is NULL using ! i.e. check if expr is false ##
[ ! -z "$my_var" ] || echo "NULL"
[ ! -z "$my_var" ] && echo "Not NULL" || echo "NULL"
 
[[ ! -z "$my_var" ]] || echo "NULL"
[[ ! -z "$my_var" ]] && echo "Not NULL" || echo "NULL"

## Check if $my_var is NULL using ! i.e. check if expr is false ## [ ! -z "$my_var" ] || echo "NULL" [ ! -z "$my_var" ] && echo "Not NULL" || echo "NULL" [[ ! -z "$my_var" ]] || echo "NULL" [[ ! -z "$my_var" ]] && echo "Not NULL" || echo "NULL"

How to find NULL value in shell if condition on Unix

The -n returns TRUE if the length of STRING is nonzero. For example:

#!/bin/bash
var="$1"
if [ ! -n "$var" ]
then
	echo "$0 - Error $var not set or NULL"
else
	echo "$var set and now starting $0 shell script..."
fi

#!/bin/bash var="$1" if [ ! -n "$var" ] then echo "$0 – Error $var not set or NULL" else echo "$var set and now starting $0 shell script…" fi

Example: Determine if a bash variable is NULL

The following shell script shows various possibilities with bash/sh/posix based shell:

#!/bin/bash
# Purpose - Test for NULL/empty shell var
# Author - SXI ADMIN {htttps://sxi.io} under GPL 2.0+
DEMO="sxi.io"
HINT=""
#####################################
# Do three possibilities for $DEMO ##
#####################################
for i in 1 2 3 
do
case $i in
	1) DEMO="nixcraft.com"; HINT="value set";;
	2) DEMO=""; HINT="value set to empty string";;
	3) unset DEMO; HINT="$DEMO unset";;
esac
 
###############################################
# Update user with actual values and action  
# $DEMO set to a non-empty string (1)
# $DEMO set to the empty string  (2)
# $DEMO can be unset (3)
################################################
echo "*** Current value of $DEMO is '$DEMO' ($HINT) ***"
 
########################################################################
## Determine if a bash variable is NULL, set, unset or not set at ALL ##
########################################################################
if [ -z "${DEMO}" ]; then
    echo "DEMO is unset or set to the empty string"
fi
if [ -z "${DEMO+set}" ]; then
    echo "DEMO is unset"
fi
if [ -z "${DEMO-unset}" ]; then
    echo "DEMO is set to the empty string"
fi
if [ -n "${DEMO}" ]; then
    echo "DEMO is set to a non-empty string"
fi
if [ -n "${DEMO+set}" ]; then
    echo "DEMO is set, possibly to the empty string"
fi
if [ -n "${DEMO-unset}" ]; then
    echo "DEMO is either unset or set to a non-empty string"
fi
done

#!/bin/bash # Purpose – Test for NULL/empty shell var # Author – SXI ADMIN {htttps://sxi.io} under GPL 2.0+ DEMO="sxi.io" HINT="" ##################################### # Do three possibilities for $DEMO ## ##################################### for i in 1 2 3 do case $i in 1) DEMO="nixcraft.com"; HINT="value set";; 2) DEMO=""; HINT="value set to empty string";; 3) unset DEMO; HINT="$DEMO unset";; esac ############################################### # Update user with actual values and action # $DEMO set to a non-empty string (1) # $DEMO set to the empty string (2) # $DEMO can be unset (3) ################################################ echo "*** Current value of $DEMO is ‘$DEMO’ ($HINT) ***" ######################################################################## ## Determine if a bash variable is NULL, set, unset or not set at ALL ## ######################################################################## if [ -z "${DEMO}" ]; then echo "DEMO is unset or set to the empty string" fi if [ -z "${DEMO+set}" ]; then echo "DEMO is unset" fi if [ -z "${DEMO-unset}" ]; then echo "DEMO is set to the empty string" fi if [ -n "${DEMO}" ]; then echo "DEMO is set to a non-empty string" fi if [ -n "${DEMO+set}" ]; then echo "DEMO is set, possibly to the empty string" fi if [ -n "${DEMO-unset}" ]; then echo "DEMO is either unset or set to a non-empty string" fi done

Conclusion

I suggest that you read the following resources for more info or see GNU/bash man page here:

(adsbygoogle = window.adsbygoogle || []).push({});

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

14/08/2019

How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...
14/08/2019

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....
12/08/2019

How to Start and Enable Firewalld on CentOS 7

In this article, we discuss how to start and enable firewalld. It is highly recommended that you have a firewall protecting your server.Pre-Flight CheckThese...