2 SC2283
Joachim Ansorg edited this page 2021-11-12 20:00:00 +01:00

Use [ ] to compare values, or remove spaces around = to assign (or quote '=' if literal).

Problematic code:

# Assignment
var = value

# Comparison
if $var = value
then
  echo "Match"
fi

Correct code:

# Assignment
var=value

# Comparison
if [ "$var" = value ]
then
  echo "Match"
fi

Rationale:

ShellCheck found an unquoted = after a word.

If this was supposed to be a comparison, use square brackets: [ "$var" = value ]

If this was supposed to be an assignment, remove spaces around =: var=value

Exceptions:

If the = was meant literally, quote it:

grep '=true' file.cfg
  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!