From 2cee504c39927a3bb305c231674917cbc2f41e92 Mon Sep 17 00:00:00 2001 From: koalaman Date: Sat, 18 Jul 2015 10:02:23 -0700 Subject: [PATCH] Created SC2157 (markdown) --- SC2157.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 SC2157.md diff --git a/SC2157.md b/SC2157.md new file mode 100644 index 0000000..506c157 --- /dev/null +++ b/SC2157.md @@ -0,0 +1,25 @@ +## Argument to implicit -n is always true due to literal strings. + +### Problematic code: + + if [ "$foo " ] + then + echo "this is always true" + fi + +### Correct code: + + if [ "$foo" ] + then + echo "correctly checks value" + fi + +### Rationale: + +Since `[ str ]` checks that the string is non-empty, the space inside the quotes in the problematic code causes the test to always be true. + +Sometimes this is also caused by overquoting an example, e.g. `[ "$foo -gt 0" ]`, which is always true for the same reason. The intention here was `[ "$foo" -gt 0 ]`. + +### Exceptions: + +None. \ No newline at end of file