Constant Comparison Medium

Constant expressions in shell comparisons often indicate a missing '$' symbol for variable references. To fix this, add the '$' symbol before variable names in comparison expressions.

Detector ID
shell/constant-comparison@v1.0
Category
Common Weakness Enumeration (CWE) external icon
-
Tags
-

Noncompliant example

1
2# Noncompliant: Compares literal strings `count` and `5`, not the value of `count`
3if [ count -eq 5 ]
4then
5  echo "Count is 5"
6fi

Compliant example

1
2# Compliant: Correctly compares the value of `$count` with `5`
3if [ "$count" -eq 5 ]
4then
5  echo "Count is 5"
6fi