Created SC2157 (markdown)

koalaman
2015-07-18 10:02:23 -07:00
parent bbdf114f12
commit 2cee504c39

25
SC2157.md Normal file

@@ -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.