From 16c5cd5fcd050a7ea32367490932c45d383b59b7 Mon Sep 17 00:00:00 2001 From: koalaman Date: Thu, 13 Jul 2017 14:16:11 -0700 Subject: [PATCH] Created SC2050 (markdown) --- SC2050.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 SC2050.md diff --git a/SC2050.md b/SC2050.md new file mode 100644 index 0000000..e9b4c6c --- /dev/null +++ b/SC2050.md @@ -0,0 +1,33 @@ +## This expression is constant. Did you forget the `$` on a variable? +### Problematic code: + +```sh +if [ myvar = "test" ] +then + echo "Test mode" +fi +``` + +### Correct code: + +```sh +if [ "$myvar" = "test" ] +then + echo "Test mode" +fi +``` +### Rationale: + +ShellCheck has found a `[ .. ]` 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 [[ "myvar" = "test" ]] # always false because myvar is a literal string + if [[ "$myvar" = "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 + +### Exceptions: + +None \ No newline at end of file