From 1494c00aae85f90a0fba414c766a927ec4c5ddcf Mon Sep 17 00:00:00 2001 From: koalaman Date: Sat, 7 Jun 2014 13:43:32 -0700 Subject: [PATCH] Created SC2145 (markdown) --- SC2145.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 SC2145.md diff --git a/SC2145.md b/SC2145.md new file mode 100644 index 0000000..290ac4f --- /dev/null +++ b/SC2145.md @@ -0,0 +1,23 @@ +## Argument mixes string and array. Use * or separate argument. + +### Problematic code: + + printf "Error: %s\n" "Bad parameters: $@" + +### Correct code: + + printf "Error: %s\n" "Bad parameters: $*" + +### Rationale: + +The behavior when concatenating a string and array is rarely intended. The preceeding string is prefixed to the first array element, while the succeeding string is appended to the last one. The middle array elements are unaffected. + +E.g., with the parameters `foo`,`bar`,`baz`, `"--flag=$@"` is equivalent to the three arguments `"--flag=foo" "bar" "baz"`. + +If the intention is to concatenate all the array elements into one argument, use `$*`. This concatenates based on `IFS`. + +If the intention is to provide each array element as a separate argument, put the array expansion in its own argument. + +### Contraindications + +None. \ No newline at end of file