Updated SC2048 (markdown)

Vidar Holen
2021-04-24 16:05:10 -07:00
parent 5dc39d699c
commit 41d86317ab

@@ -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.