Skip to content
Joachim Ansorg edited this page Nov 12, 2021 · 5 revisions

Use "$@" (with quotes) to prevent whitespace problems.

Or: Use "${array[@]}" (with quotes) to prevent whitespace problems.

Problematic code:

cp $* ~/dir
cp ${array[*]} ~/dir

Correct code:

cp "$@" ~/dir
cp "${array[@]}" ~/dir

Rationale:

$* and ${array[*]}, unquoted, is subject to word splitting and globbing.

Let's say you have three arguments or array elements: baz, foo bar and *

"$@" and "${array[@]}" will expand into exactly that: baz, foo bar and *

$* and ${array[*]} will expand into multiple other arguments: baz, foo, bar, file.txt and otherfile.jpg

Since the latter is rarely expected or desired, ShellCheck warns about it.

Exceptions

None.

ShellCheck

Each individual ShellCheck warning has its own wiki page like SC1000. Use GitHub Wiki's "Pages" feature above to find a specific one, or see Checks.

Clone this wiki locally