mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC2145 (markdown)
23
SC2145.md
Normal file
23
SC2145.md
Normal file
@@ -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.
|
Reference in New Issue
Block a user