mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC2068 (markdown)
25
SC2068.md
Normal file
25
SC2068.md
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
## Add double quotes around ${@}, otherwise it's just like $* and breaks on spaces.
|
||||||
|
|
||||||
|
### Problematic code:
|
||||||
|
|
||||||
|
cp $@ ~/dir
|
||||||
|
|
||||||
|
### Correct code:
|
||||||
|
|
||||||
|
cp "$@" ~/dir
|
||||||
|
|
||||||
|
### Rationale:
|
||||||
|
|
||||||
|
Double quotes around `$@` prevents globbing and word splitting while still expanding to multiple separate arguments.
|
||||||
|
|
||||||
|
Let's say you have three 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` and `otherfile.jpg`
|
||||||
|
|
||||||
|
Since the latter is rarely expected or desired, ShellCheck warns about it.
|
||||||
|
|
||||||
|
### Contraindications
|
||||||
|
|
||||||
|
None.
|
Reference in New Issue
Block a user