From a540975a1f2f55d7e8051f89c13bcf0d95931bda Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Tue, 28 Aug 2018 20:15:13 -0700 Subject: [PATCH] Created SC2237 (markdown) --- SC2237.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 SC2237.md diff --git a/SC2237.md b/SC2237.md new file mode 100644 index 0000000..510bd00 --- /dev/null +++ b/SC2237.md @@ -0,0 +1,37 @@ +## Use `[ -n .. ]` instead of `! [ -z .. ]`. + +(or "Use `[ -z .. ]` instead of `! [ -n .. ]`.) + +### Problematic code: + +```sh +if ! [ -n "$JAVA_HOME" ]; then echo "JAVA_HOME not specified"; fi +if ! [ -z "$STY" ]; then echo "You are already running screen"; fi +``` + +### Correct code: + +```sh +if [ -z "$JAVA_HOME" ]; then echo "JAVA_HOME not specified"; fi +if [ -n "$STY" ]; then echo "You are already running screen"; fi +``` + +### Rationale: + +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 + +### Exceptions: + +This is a stylistic issue that does not affect correctness. If you prefer the original expression, you can't not [[Ignore]] it with a directive or flag. + +### Related resources: + +* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc! \ No newline at end of file