Testing if a variable is empty in a shell script

I have seen the following technique used many times on many different shells, to test if a variable is empty:

if [ "x$1" = "x" ]; then 
    # Variable is empty
fi

Are there any advantages on using this over the more canonical if [ -z "$1" ]? Could it be a portability issue?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Some historical shells implemented a very simple parser that could get confused by things like [ -n = "" ] where the first operand to = looks like an operator, and would parse this as [ -n = ] or cause a syntax error. In [ "x$1" = x"" ], the x prefix ensures that x"$1" cannot possibly look like an operator, and so the only way the shell can parse this test is by treating = as a binary operator.

All modern shells, and even most older shells still in operation, follow the POSIX rules which mandate that all test expressions of up to 4 words be parsed correctly. So [ -z "$1" ] is a proper way of testing if $1 is empty, and [ "$x" = "$y" ] is a proper way to test the equality of two variables.

Even some current shells can get confused with longer expressions, and a few expressions are actually ambiguous, so avoid using the -a and -o operators to construct longer boolean tests, and instead use separate calls to [ and the shell’s own && and || boolean operators.

Method 2

According to http://www.mpi-inf.mpg.de/~uwe/lehre/unixffb/quoting-guide.html, the -z test is unsafe in some implementations, presumably when “interesting” strings like "-o a=a" are tested.

Method 3

The above tests will also cause an error if you run with “set -u” or “set -o nounset”

A more stable way to check for an empty variable would be to use parameter expansion:

MYVAR=${MYVAR:-“Bad Value”}

This method works for the traditional bourne shell, as well as ksh, and bash.

Method 4

    function isBlank {
 valueNoSpaces=$(echo "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7f5b3f">[email protected]</a>" | tr -d ' ')

 if [  "$valueNoSpaces" == null ] || [ -z "$valueNoSpaces" ] 
 then
       echo true ;
 else
       echo ""  ;
 fi
}

#Test
if [ $(isBlank "      ") ] 
then
    echo "isBlank "      " : it's blank"
else
    echo " isBlank "      ": it is not blank"
fi

if [ $(isBlank "abc") ] 

then
    echo "isBlank "abc" : it's blank"
else
    echo "isBlank "abc" :it is not blank"
fi

if [ $(isBlank null) ] 
then
      echo "isBlank null : it's blank"
else
    echo "isBlank null : it is not blank"
fi

if [ $(isBlank "") ] 
then
    echo "isBlank "" : it's blank"
else
    echo "isBlank "" : it is not blank"
fi

#Result
isBlank "      " : it's blank

isBlank "abc" :it is not blank

isBlank null : it's blank

isBlank "" : it's blank


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x