Site icon Techolac – Computer Technology News

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

[ad_1]

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

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 - Vivek Gite {htttps://www.cyberciti.biz} under GPL 2.0+
DEMO="cyberciti.biz"
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 – Vivek Gite {htttps://www.cyberciti.biz} under GPL 2.0+
DEMO=”cyberciti.biz”
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:

Posted by: Vivek Gite

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.

[ad_2]

Exit mobile version