- 
                Notifications
    
You must be signed in to change notification settings  - Fork 1.9k
 
SC2236
        Eisuke Kawashima edited this page Jul 31, 2025 
        ·
        6 revisions
      
    (or "Use -z instead of ! -n")
if [ ! -n "$JAVA_HOME" ]; then echo "JAVA_HOME not specified"; fi
if [ ! -z "$STY" ];       then echo "You are already running screen"; fiif [ -z "$JAVA_HOME" ]; then echo "JAVA_HOME not specified"; fi
if [ -n "$STY" ];       then echo "You are already running screen"; fiYou 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 emptyThis is a stylistic issue that does not affect correctness. If you prefer the original expression, you can Ignore it with a directive or flag.
- Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
 - Bash reference manual for -z and -n.
 - Note: Be careful with quoting variables (which you should always do anyway): 
[ ! -z $var ]might work, but[ -n $var]will not.[ -n "$var" ]will do what you expect.