From f4f08bbfb98108aeba93d9656de2b333d9a9678a Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Wed, 30 Dec 2020 20:23:49 -0800 Subject: [PATCH] Created SC2283 (markdown) --- SC2283.md | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 SC2283.md diff --git a/SC2283.md b/SC2283.md new file mode 100644 index 0000000..3cbc5e7 --- /dev/null +++ b/SC2283.md @@ -0,0 +1,45 @@ +## Use [ ] to compare values, or remove spaces around = to assign (or quote '=' if literal). + +### Problematic code: + +```sh +# Assignment +var = value + +# Comparison +if $var = value +then + echo "Match" +fi +``` + +### Correct code: + +```sh +# 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 + +### Related resources: + +* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc! \ No newline at end of file