From 8a67f70466cfa437b1a613b438d3524e4954c3bf Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Wed, 19 Oct 2022 20:48:37 -0700 Subject: [PATCH] Created SC1076 (markdown) --- SC1076.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 SC1076.md diff --git a/SC1076.md b/SC1076.md new file mode 100644 index 0000000..ebe7c62 --- /dev/null +++ b/SC1076.md @@ -0,0 +1,26 @@ +## Trying to do math? Use e.g. `[ $((i/2+7)) -ge 18 ]`. + +### Problematic code: + +```sh +[ i / 2 + 7 -ge 18 ] +``` + +### Correct code: + +```sh +[ $((i / 2 + 7)) -ge 18 ] +``` +### Rationale: + +ShellCheck found a loose `+*/%` in a test statement. This usually happens when trying to do arithmetic in a condition, but without using the arithmetic expansion construct `$((expression))`. + +In C, `if (a+b == c)` is perfectly fine, but in sh this must be written to first expand the arithmetic operation like `if [ $((a+b)) = c ]`. + +### Exceptions: + +None. + +### Related resources: + +* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc! \ No newline at end of file