Checking for Null String in Bash

Shell Script : How to check if variable is null or no

Try following, you should change from -z to -n as follows and add $ to your variable too.

if [[ -n "$list_Data" ]]
then
echo "not Empty"
else
echo "empty"
fi

Explanation: From man test page as follows(It checks if a variable is having any value or not. If it has any value then condition is TRUE, if not then it is FALSE.)

   -n STRING
the length of STRING is nonzero

Check if string is neither empty nor space in shell script

You need a space on either side of the !=. Change your code to:

str="Hello World"
str2=" "
str3=""

if [ ! -z "$str" -a "$str" != " " ]; then
echo "Str is not null or space"
fi

if [ ! -z "$str2" -a "$str2" != " " ]; then
echo "Str2 is not null or space"
fi

if [ ! -z "$str3" -a "$str3" != " " ]; then
echo "Str3 is not null or space"
fi

Null & empty string comparison in Bash

First of all, note you are not using the variable correctly:

if [ "pass_tc11" != "" ]; then
# ^
# missing $

Anyway, to check if a variable is empty or not you can use -z --> the string is empty:

if [ ! -z "$pass_tc11" ]; then
echo "hi, I am not empty"
fi

or -n --> the length is non-zero:

if [ -n "$pass_tc11" ]; then
echo "hi, I am not empty"
fi

From man test:

-z STRING

the length of STRING is zero

-n STRING

the length of STRING is nonzero

Samples:

$ [ ! -z "$var" ] && echo "yes"
$

$ var=""
$ [ ! -z "$var" ] && echo "yes"
$

$ var="a"
$ [ ! -z "$var" ] && echo "yes"
yes

$ var="a"
$ [ -n "$var" ] && echo "yes"
yes

Bash script not checking for null correctly

The "null string" is an empty string -- a zero-byte string, that is, a string with no characters contained within it. s="" assigns a null string to the variable s, just as s= or s='' do.

s=null, or s='null', or s="null", by contrast, assign a string with the four-byte string null (and then terminated by a literal NUL character, as all C strings are). These strings are not "null strings" or "empty strings" in the sense that test -z "$s" or [ -z "$s" ] check for: It doesn't have zero characters; instead, it has four (those being n, u, l and l).

To detect either an empty string or the non-empty string "null", use instead:

if [ -z "$someId" ] || [ "$someId" = null ]; then
echo "Order Placement failed; unable to parse someId from the response"
exit 1
fi

How to find whether or not a variable is empty in Bash

In Bash at least the following command tests if $var is empty:

if [[ -z "$var" ]]; then
# $var is empty, do what you want
fi

The command man test is your friend.

Checking for null string in bash

Nope, they are all the same. But couple of defensive habits to get into.

  • You should quote the $STRING in the -z one as well
  • If you are running with the -u option (I always do) then you should reference the possibly optional variable as ${STRING-} just in case its not set at all

How do I test for an empty string in a Bash case statement?

The case statement uses globs, not regexes, and insists on exact matches.

So the empty string is written, as usual, as "" or '':

case "$command" in
"") do_empty ;;
something) do_something ;;
prefix*) do_prefix ;;
*) do_other ;;
esac


Related Topics



Leave a reply



Submit