mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC2157 (markdown)
25
SC2157.md
Normal file
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.
|
Reference in New Issue
Block a user