Skip to content

Files

Latest commit

 

History

History
37 lines (25 loc) · 910 Bytes

SC2050.md

File metadata and controls

37 lines (25 loc) · 910 Bytes

Pattern: Constant [ .. ] or [[ .. ]] comparison

Issue: -

Description

This rule checks for [ .. ] or [[ .. ]] comparison that only involves literal strings. The intention was probably to check a variable or command output instead.

This is usually due to missing $ or bad quoting:

if [[ "somevar" = "test" ]]             # always false because somevar is a literal string
if [[ "$somevar" = "test" ]]            # correctly compares a variable

if [ 'grep -c foo bar' -ge 10 ]       # always false because grep doesn't run
if [ "$(grep -c foo bar)" -ge 10 ]    # correctly checks grep output

Example of incorrect code:

if [ somevar = "test" ]
then
  echo "Test mode"
fi

Example of correct code:

if [ "$somevar" = "test" ]
then
  echo "Test mode"
fi

Further Reading