From 44553c5b1abe53a81dcd885e06327791c8e6c08c Mon Sep 17 00:00:00 2001 From: koalaman Date: Mon, 27 Apr 2015 16:58:53 -0700 Subject: [PATCH] Created SC2036 (markdown) --- SC2036.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 SC2036.md diff --git a/SC2036.md b/SC2036.md new file mode 100644 index 0000000..d12b8d8 --- /dev/null +++ b/SC2036.md @@ -0,0 +1,23 @@ +## If you wanted to assign the output of the pipeline, use a=$(b | c) . + +### Problematic code: + + sum=find | wc -l + +### Correct code: + + sum=$(find | wc -l) + +### Rationale: + +The intention in this code was that `sum` would in some way get the value of the command `find | wc -l`. + +However, `|` has precedence over the `=`, so the command is a two stage pipeline consisting of `sum=find` and `wc -l`. + +`sum=find` is a plain string assignment. Since it happens by itself in an independent pipeline stage, it has no effect: it produces no output, and the variable disappears when the pipeline stage finishes. Because the assignment produces no output, `wc -l` will count 0 lines. + +To instead actually assign a variable with the output of a command, command substitution `$(..)` can be used. + +### Exceptions: + +None. This warning is triggered whenever the first stage of a pipeline is a single assignment, which is never correct. \ No newline at end of file