How Do You Troubleshoot the Bash Error “unary operator expected”?

Problem scenario
You run a Bash script with an if condition that uses the -Z flag.  It looks like this:

if [ $foovar -Z ]; then

The script seems to run with an ignorable error message "unary operator expected".  How do you get this error to not appear?

Solution
Use '= ""' instead of "-Z".  Here is one way to re-write the if condition above:

if [ $foovar = "" ]; then

Leave a comment

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