From 03b38d5f91d228e80251a968fb39639193468a31 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Thu, 28 Jun 2018 11:35:27 -0700 Subject: [PATCH] Created SC2100 (markdown) --- SC2100.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 SC2100.md diff --git a/SC2100.md b/SC2100.md new file mode 100644 index 0000000..9e99ae2 --- /dev/null +++ b/SC2100.md @@ -0,0 +1,33 @@ +## Use `$((..))` for arithmetics, e.g. `i=$((i + 2))` + +### Problematic code: + +```sh +i=3 +i=i+2 +``` + +### Correct code: + +```sh +i=3 +i=$((i + 2)) +``` + +### Rationale: + +Unlike most languages, variable assignments (almost) always assigns strings and not expressions. In the example code, `i` will become the string `i+2` instead of the intended `5`. + +To instead evaluate a mathematical expressions, use `$((..))` as in the correct code. + +### Exceptions: + +If you wanted to assign a literal string, quote it: + + description="friendly-looking" + +ShellCheck (as of v0.5) doesn't recognize Bash/Ksh numeric variables created with `declare -i` where this syntax is valid. Using `$((..))` still works, but you can also [[ignore]] this warning. + +### Related resources: + +* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc! \ No newline at end of file