Skip to content

Latest commit

 

History

History
37 lines (24 loc) · 1.05 KB

SC2236.md

File metadata and controls

37 lines (24 loc) · 1.05 KB

Pattern: Use of ! -z instead of -n

Issue: -

Description

You have negated test -z or test -n, resulting in a needless double-negative. You can just use the other operator instead:

# Identical tests to verify that a value is assigned
[ ! -z foo ] # Not has no value
[ -n foo ]   # Has value

# Identical tests to verify that a value is empty
[ ! -n foo ] # Not is non-empty
[ -z foo ]   # Is empty

Example of incorrect code:

if [ ! -n "$JAVA_HOME" ]; then echo "JAVA_HOME not specified"; fi
if [ ! -z "$STY" ];       then echo "You are already running screen"; fi

Example of correct code:

if [ -z "$JAVA_HOME" ]; then echo "JAVA_HOME not specified"; fi
if [ -n "$STY" ];       then echo "You are already running screen"; fi

Exceptions

This is a stylistic issue that does not affect correctness. If you prefer the original expression, you can ignore it with a directive or flag.

Further Reading