From c2ad75fc62a7078a4ab58f2bcc95e2dfa7fbd08b Mon Sep 17 00:00:00 2001 From: koalaman Date: Sat, 5 Dec 2015 17:26:57 -0800 Subject: [PATCH] Updated SC2070 (markdown) --- SC2070.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/SC2070.md b/SC2070.md index e866fdf..036bcca 100644 --- a/SC2070.md +++ b/SC2070.md @@ -1,4 +1,4 @@ -## Always true because you failed to quote. Use [[ ]] instead. +## -n doesn't work with unquoted arguments. Quote or use [[ ]]. ### Problematic code: @@ -13,10 +13,10 @@ fi ### Correct code: -In bash/ksh: +In POSIX: ```sh -if [[ -n $var ]] +if [ -n "$var" ] then echo "var has a value" else @@ -24,10 +24,10 @@ else fi ``` -In POSIX: +In bash/ksh: ```sh -if [ -n "$var" ] +if [[ -n $var ]] then echo "var has a value" else @@ -47,7 +47,7 @@ When `$var` is unquoted, a blank value will cause it to wordsplit and disappear. `[ string ]` is shorthand for testing if a string is empty. This is still true if `string` happens to be `-n`. `[ -n ]` is therefore true, and by extension so is `[ -n $var ]`. -To fix this, either use `[[ -n $var ]]` which has fewer caveats than `[`, or quote the variable. +To fix this, either quote the variable, or (if your shell supports it) use `[[ -n $var ]]` which generally has fewer caveats than `[`. ### Exceptions: