Testing whether a string is null.

Testing whether a string is null.

#!/bin/bash
# str-test.sh: Testing null strings and unquoted strings,
#+ but not strings and sealing wax, not to mention cabbages and kings . . .

# Using if [ … ]

# If a string has not been initialized, it has no defined value.
# This state is called “null” (not the same as zero!).

if [ -n $string1 ] # string1 has not been declared or initialized.
then
echo “String “string1″ is not null.”
else
echo “String “string1″ is null.”
fi # Wrong result.
# Shows $string1 as not null, although it was not initialized.

echo

# Let’s try it again.

if [ -n “$string1” ] # This time, $string1 is quoted.
then
echo “String “string1″ is not null.”
else
echo “String “string1″ is null.”
fi # Quote strings within test brackets!

echo

if [ $string1 ] # This time, $string1 stands naked.
then
echo “String “string1″ is not null.”
else
echo “String “string1″ is null.”
fi # This works fine.
# The [ … ] test operator alone detects whether the string is null.
# However it is good practice to quote it (if [ “$string1” ]).
#
# As Stephane Chazelas points out,
# if [ $string1 ] has one argument, “]”
# if [ “$string1” ] has two arguments, the empty “$string1” and “]”

echo

string1=initialized

if [ $string1 ] # Again, $string1 stands unquoted.
then
echo “String “string1″ is not null.”
else
echo “String “string1″ is null.”
fi # Again, gives correct result.
# Still, it is better to quote it (“$string1″), because . . .

string1=”a = b”

if [ $string1 ] # Again, $string1 stands unquoted.
then
echo “String “string1″ is not null.”
else
echo “String “string1″ is null.”
fi # Not quoting “$string1” now gives wrong result!

exit 0

Copied from http://tldp.org/LDP/abs/html/comparison-ops.html#STRTEST

Leave a Reply

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

*