Skip to content
Atanas Yankov edited this page Sep 1, 2020 · 7 revisions

Double quote array expansions to avoid re-splitting elements.

Problematic code:

cp $@ ~/dir

Correct code:

cp "$@" ~/dir

Rationale:

Double quotes around $@ (and similarly, ${array[@]}) prevents globbing and word splitting of individual elements, while still expanding to multiple separate arguments.

Let's say you have four arguments: baz, foo bar, * and /*/*/*/*

"$@" will expand into exactly that: baz, foo bar, * and /*/*/*/*

$@ will expand into multiple other arguments: baz, foo, bar, file.txt, otherfile.jpg, and (eventually) a list of most files on the system

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

Exceptions

When you want globbing of individual elements.

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