Skip to content

Latest commit

 

History

History
31 lines (21 loc) · 716 Bytes

SC2157.md

File metadata and controls

31 lines (21 loc) · 716 Bytes

Pattern: Constant string match in shell script

Issue: -

Description

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 over-quoting an example, e.g. [ "$foo -gt 0" ], which is always true for the same reason. The intention here was [ "$foo" -gt 0 ].

Example of incorrect code:

if [ "$foo " ]
then
  echo "this is always true"
fi

Example of correct code:

if [ "$foo" ]
then
  echo "correctly checks value"
fi

Further Reading