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