Skip to content
Vidar Holen edited this page Oct 4, 2015 · 5 revisions

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, since a string with a space can not be empty.

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.

ShellCheck

Each individual ShellCheck warning has its own wiki page like SC1000. Use GitHub Wiki's "Pages" feature above to find a specific one, or see Checks.

Clone this wiki locally