Linux Tutorials

Bash shell discover out if a variable has NULL worth OR not

SEOClerks

I am a brand new to shell scripting. How do I test for NULL worth in a Linux or Unix shell scripts?

You possibly can rapidly check for null or empty variables in a Bash shell script. It’s essential to cross the -z or -n choice to the check command or to the if command or use conditional expression. This web page reveals the right way to discover out if a bash shell variable has NULL worth or not utilizing the check command.

To search out out if a bash variable is null:

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”; fiAnother possibility to search out if bash variable set to NULL: [ -z “$var” ] && echo “NULL”Decide if a bash variable is NULL: [[ ! -z “$var” ]] && echo “Not NULL” || echo “NULL”

Bash shell discover out if a variable has NULL worth OR not

Allow us to see check command syntax and examples in particulars. 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

OR

my_var=“”
if check -z $my_var
then
echo $my_var is NULL”
else
echo $my_var is NOT NULL”
fi

One other choice to test if bash shell variable is NULL or not

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

OR

## Test if $my_var is NULL utilizing ! i.e. test if expr is fake ##
[ ! -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”

Find out how to discover NULL worth in shell if situation on Unix

The -n returns TRUE if the size of STRING is nonzero. For instance:

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

Instance: Decide if a bash variable is NULL

The next shell script reveals varied potentialities with bash/sh/posix based mostly shell:

#!/bin/bash
# Objective – Take a look at for NULL/empty shell var
# Creator – Vivek Gite underneath GPL 2.zero+
DEMO=“cyberciti.biz”
HINT=“”
#####################################
# Do three potentialities for $DEMO ##
#####################################
for i in 1 2 three
do
case $i in
1) DEMO=“nixcraft.com”; HINT=“worth set”;;
2) DEMO=“”; HINT=“worth set to empty string”;;
three) unset DEMO; HINT=$DEMO unset”;;
esac
 
###############################################
# Replace consumer with precise values and motion
# $DEMO set to a non-empty string (1)
# $DEMO set to the empty string (2)
# $DEMO may be unset (three)
################################################
echo “*** Present worth of $DEMO is ‘$DEMO‘ ($HINT) ***”
 
########################################################################
## Decide if a bash variable is NULL, set, unset or not set at ALL ##
########################################################################
if [ -z $ ]; then
echo “DEMO is unset or set to the empty string”
fi
if [ -z $ ]; then
echo “DEMO is unset”
fi
if [ -z $DEMO-unset ]; then
echo “DEMO is about to the empty string”
fi
if [ -n $ ]; then
echo “DEMO is about to a non-empty string”
fi
if [ -n $ ]; then
echo “DEMO is about, presumably to the empty string”
fi
if [ -n $DEMO-unset ]; then
echo “DEMO is both unset or set to a non-empty string”
fi
carried out

Bash Shell Find Out If a Variable Is NULL

Conclusion

I recommend that you just learn the next sources for more information or see GNU/bash man web page right here:

Posted by: Vivek Gite

The creator is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a coach for the Linux working system/Unix shell scripting. Get the newest tutorials on SysAdmin, Linux/Unix and open supply matters through RSS/XML feed or weekly electronic mail e-newsletter.

Source link

Related Articles

Back to top button