From ba80e675fcfcbfb4ba317f49ccf74c244c876a99 Mon Sep 17 00:00:00 2001 From: koalaman Date: Mon, 9 Jan 2017 23:37:51 -0800 Subject: [PATCH] Created SC2204 (markdown) --- SC2204.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 SC2204.md diff --git a/SC2204.md b/SC2204.md new file mode 100644 index 0000000..654c8dc --- /dev/null +++ b/SC2204.md @@ -0,0 +1,32 @@ +## (..) is a subshell. Did you mean [ .. ], a test expression? + +### Problematic code: + +```sh +if ( -d mydir ) +then + echo "It's a directory" +fi +``` + +### Correct code: + +```sh +if [ -d mydir ] +then + echo "It's a directory" +fi +``` +### Rationale: + +Tests like `-d` to see if something is a directory or `-z` to see if it's non-empty are actually flags 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 `[ -d mydir ]`. + +`( .. )` 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: + +None. + +This error is triggered by having a unary test operator as the first command name in a subshell, which won't normally happen. Note that there's a similar warning [[SC2205]] with a higher false positive rate. \ No newline at end of file