From af16a6d94927c8646fc335a3c01b9fc03866f273 Mon Sep 17 00:00:00 2001 From: koalaman Date: Mon, 9 Jan 2017 23:43:28 -0800 Subject: [PATCH] Created SC2205 (markdown) --- SC2205.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 SC2205.md diff --git a/SC2205.md b/SC2205.md new file mode 100644 index 0000000..6835cb0 --- /dev/null +++ b/SC2205.md @@ -0,0 +1,32 @@ +## (..) is a subshell. Did you mean [ .. ], a test expression? + +### Problematic code: + +```sh +if ( 1 -lt 2 ) +then + echo "1 is less than 2" +fi +``` + +### Correct code: + +```sh +if [ 1 -lt 2 ] +then + echo "1 is less than 2" +fi +``` +### Rationale: + +Tests like `-eq` to check numeric equality or `\<` for string comparison only work are actually parameters to the `test` command, and only work as tests in that context. `[` is an alias for `test`, so you'll frequently see them written as `[ 1 -eq 2 ]`. + +`( .. )` is completely unrelated, and is a subshell mostly used to scope shell modifications. They should not be used in `if` or `while` statements in shell scripts. + +If you wanted to test a condition, rewrite the `( .. )` to `[ .. ]`. + +### Exceptions: + +This error is triggered by having a binary operator as the first parameter in a subshell, and could falsely trigger on e.g. `if ( grep -eq "foo|bar" file )`. In these cases, check whether the subshell is actually needed. + +Note that there's a similar looking error [[SC2204]] with a low false positive rate. \ No newline at end of file