From 90e49fff00f0d3f1995efdb399539776cf0966a3 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Tue, 22 May 2018 22:53:23 -0700 Subject: [PATCH] Created SC2233 (markdown) --- SC2233.md | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 SC2233.md diff --git a/SC2233.md b/SC2233.md new file mode 100644 index 0000000..4477320 --- /dev/null +++ b/SC2233.md @@ -0,0 +1,43 @@ +## Remove superfluous `(..)` around condition. + +### Problematic code: + +```sh +if ([ "$x" -gt 0 ]) +then true; fi +``` + +### Correct code: + +```sh +if [ "$x" -gt 0 ] +then true; fi +``` + +### Rationale: + +The shell syntax is `if cmd`, `elif cmd`, `while cmd` and `until cmd` without any parentheses. Instead, parentheses are an independent construct used to create subshells. + +ShellCheck has noticed that you're wrapping `(..)` around one or more test commands. This is unnecessary, and the resulting fork adds quite a lot of overhead: + +``` +$ i=0; time while ( [ "$i" -lt 10000 ] ); do i=$((i+1)); done +real 0m6.998s +user 0m3.453s +sys 0m3.464s + +$ i=0; time while [ "$i" -lt 10000 ]; do i=$((i+1)); done +real 0m0.055s +user 0m0.054s +sys 0m0.001s +``` + +Just delete the surrounding `(..)` since they serve no purpose and only slows the script down. + +### Exceptions: + +None. + +### Related resources: + +* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc! \ No newline at end of file