Skip to content

Files

Latest commit

 

History

History
29 lines (17 loc) · 931 Bytes

SC2145.md

File metadata and controls

29 lines (17 loc) · 931 Bytes

Pattern: Argument mixes string and array

Issue: -

Description

The behavior when concatenating a string and array is rarely intended. The preceding 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.

Example of incorrect code:

printf "Error: %s\n" "Bad parameters: $@"

Example of correct code:

printf "Error: %s\n" "Bad parameters: $*"

Further Reading