From 99219bdf939fab56fb65be3457af307df60282e1 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Fri, 9 Apr 2021 10:19:09 -0700 Subject: [PATCH] Created SC1106 (markdown) --- SC1106.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 SC1106.md diff --git a/SC1106.md b/SC1106.md new file mode 100644 index 0000000..5800666 --- /dev/null +++ b/SC1106.md @@ -0,0 +1,35 @@ +## In arithmetic contexts, use `<` instead of `-lt` + +Similarly, `>` instead of `-gt`, `<=` instead of `-le`, `>=` instead of `-ge`, `==` instead of `-eq`, `!=` instead of `-ne`. + +### Problematic code: + +```sh +if (( 2 -lt 3 )) +then + echo "True" +fi +``` + +### Correct code: + +```sh +if (( 2 < 3 )) +then + echo "True" +fi +``` + +### Rationale: + +The comparators `-lt`, `-ge`, `-eq` and friends are flags for the `test` command aka `[`. You are instead using it in an arithmetic context, such as `(( .. ))` or `$(( .. ))`, where you should be using `<`, `>=`, `==` etc instead. + +In arithmetic contexts, `-lt` is simply interpreted as "subtract the value of `$lt`", which is clearly not the intention. + +### Exceptions: + +If you do want to subtract `$lt` you can add a space to make this clear to ShellCheck: `echo $((3 - lt))` + +### Related resources: + +* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc! \ No newline at end of file