Skip to content

Files

Latest commit

 

History

History
33 lines (22 loc) · 1.15 KB

SC2193.md

File metadata and controls

33 lines (22 loc) · 1.15 KB

Pattern: Invalid value comparison in shell script

Issue: -

Description

ShellCheck has determined that the two values you're comparing can never be equal.

Most of the time, this happens because of a syntax issue that introduced unintended literal characters into one of the arguments.

Example of incorrect code:

[ $var+1 == 5 ]              # Unevaluated math
[ "{$var}" == "value" ]      # Swapped around $ and {
[ "$(cmd1) | cmd2" == "42" ] # Ended with ) too soon
[[ "$var " == *.png ]]       # Trailing space

The left-hand side in the problematic examples will always contain (respectively) curly braces, pipe and trailing space. The right-hand sides are literal values and a pattern without trailing spaces, so they will never be equal. The statement is therefore useless, strongly indicating a bug.

Example of correct code:

[ $((var+1)) == 5 ]          # Evaluated math
[ "${var}" == "value" ]      # Correct variable expansion
[ "$(cmd1 | cmd2)" == "42" ] # Correct command substitution
[[ "$var" == *.png ]]        # No trailing space

Further Reading