Techolac - Computer Technology News
  • Home
  • Internet
  • Business
  • Computers
  • Gadgets
  • Lifestyle
  • Phones
  • Travel
  • Tech
  • More
    • Automotive
    • Education
    • Entertainment
    • Health
    • SEO
    • Linux
    • WordPress
    • Home Improvement
    • How to
    • Games
No Result
View All Result
  • Home
  • Internet
  • Business
  • Computers
  • Gadgets
  • Lifestyle
  • Phones
  • Travel
  • Tech
  • More
    • Automotive
    • Education
    • Entertainment
    • Health
    • SEO
    • Linux
    • WordPress
    • Home Improvement
    • How to
    • Games
No Result
View All Result
Techolac - Computer Technology News
No Result
View All Result
Home Linux

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

by Editorial Staff
December 27, 2018
in Linux
Reading Time: 10 mins read

[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.

Table of Contents
To find out if a bash variable is null:
Bash shell find out if a variable has NULL value OR not
Another option to check if bash shell variable is NULL or not
How to find NULL value in shell if condition on Unix
Example: Determine if a bash variable is NULL
Conclusion
Posted by: Vivek Gite

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

Bash Shell Find Out If a Variable Is NULL

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]

Related Posts

Apache, MySQL Performance

How to Optimize Apache, MySQL Performance for 1GB RAM VPS Centos/RHEL

April 7, 2024
Top 5 MySQL Performance Tuning Tips

Top 5 MySQL Performance Tuning Tips

January 25, 2023

Top Commands to Check the Running Processes in Linux

May 27, 2022

4 Tips to Prevent and Troubleshoot the ImagePullBackOff Kubernetes Error

March 30, 2022

How Common Signals are Used in Kubernetes Pod Management

November 20, 2021

How to Transfer Windows 10 to SSD?

April 14, 2022

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Articles

  • How Smart Incident Reporting Tools Are Reshaping Modern Workplace Safety
  • 16 Free Password Manager for Mac in 2025
  • 13 HIPAA Compliant Credit Card Processing in 2025
  • Embracing EdTech: The Rise of Subtitle Generator in Online Classrooms
  • 20 Best Letflix Alternatives to Watch Movies and Tv Shows
  • Five Key Factors to Consider When Choosing Industrial Connectors
  • Best 15 HR (Human Resource) Software in 2025

Related Posts

None found

  • DashTech
  • TechDaddy
  • Terms and Conditions
  • Disclaimer
  • Write for us

© Techolac © Copyright 2019 - 2022, All Rights Reserved.

No Result
View All Result
  • Home
  • Internet
  • Business
  • Computers
  • Gadgets
  • Lifestyle
  • Phones
  • Travel
  • Tech
  • More
    • Automotive
    • Education
    • Entertainment
    • Health
    • SEO
    • Linux
    • WordPress
    • Home Improvement
    • How to
    • Games

© Techolac © Copyright 2019 - 2022, All Rights Reserved.