mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Updated SC2048 (markdown)
12
SC2048.md
12
SC2048.md
@@ -1,26 +1,30 @@
|
|||||||
## Use "$@" (with quotes) to prevent whitespace problems.
|
## Use "$@" (with quotes) to prevent whitespace problems.
|
||||||
|
|
||||||
|
Or: Use "${array[@]}" (with quotes) to prevent whitespace problems.
|
||||||
|
|
||||||
### Problematic code:
|
### Problematic code:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
cp $* ~/dir
|
cp $* ~/dir
|
||||||
|
cp ${array[*]} ~/dir
|
||||||
```
|
```
|
||||||
|
|
||||||
### Correct code:
|
### Correct code:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
cp "$@" ~/dir
|
cp "$@" ~/dir
|
||||||
|
cp "${array[@]}" ~/dir
|
||||||
```
|
```
|
||||||
|
|
||||||
### Rationale:
|
### Rationale:
|
||||||
|
|
||||||
`$*`, unquoted, is subject to word splitting and globbing.
|
`$*` and `${array[*]}`, unquoted, is subject to word splitting and globbing.
|
||||||
|
|
||||||
Let's say you have three arguments: `baz`, `foo bar` and `*`
|
Let's say you have three arguments or array elements: `baz`, `foo bar` and `*`
|
||||||
|
|
||||||
`"$@"` will expand into exactly that: `baz`, `foo bar` and `*`
|
`"$@"` and `"${array[@]}" `will expand into exactly that: `baz`, `foo bar` and `*`
|
||||||
|
|
||||||
`$*` will expand into multiple other arguments: `baz`, `foo`, `bar`, `file.txt` and `otherfile.jpg`
|
`$*` 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.
|
Since the latter is rarely expected or desired, ShellCheck warns about it.
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user