From 7afa63dc335b226f63864067cc3ee5a503e6acaf Mon Sep 17 00:00:00 2001 From: koalaman Date: Sun, 21 May 2017 10:44:21 -0700 Subject: [PATCH] Created SC2078 (markdown) --- SC2078.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 SC2078.md diff --git a/SC2078.md b/SC2078.md new file mode 100644 index 0000000..91b6084 --- /dev/null +++ b/SC2078.md @@ -0,0 +1,35 @@ +## This expression is constant. Did you forget a `$` somewhere? +### Problematic code: + +```sh +if [ "myvar" ] +then + echo "myvar is set" +fi +``` + +### Correct code: + +```sh +if [ "$myvar" ] +then + echo "myvar is set" +fi +``` +### Rationale: + +ShellCheck has found a `[ .. ]` or `[[ .. ]]` statement that just contains a literal string. Such a check does not do anything useful, and will always be true (or always false, for empty strings). + +This is usually due to missing `$` or bad quoting: + + if [[ STY ] # always true + if [[ $STY ]] # checks variable $STY + + if [[ 'grep foo bar' ]] # always true + if [[ `grep foo bar` ]] # checks grep output (poorly) + if grep -q foo bar # checks for grep match (preferred) + + +### Exceptions: + +None \ No newline at end of file