mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 11:19:45 +08:00
Updated SC2207 (markdown)
14
SC2207.md
14
SC2207.md
@@ -8,12 +8,6 @@ array=( $(mycommand) )
|
||||
|
||||
### Correct code:
|
||||
|
||||
If the output should be a single element:
|
||||
|
||||
```sh
|
||||
array=( "$(mycommand)" )
|
||||
```
|
||||
|
||||
If it outputs multiple lines, each of which should be an element:
|
||||
|
||||
```sh
|
||||
@@ -34,14 +28,20 @@ IFS=" " read -r -a array <<< "$(mycommand)"
|
||||
IFS=" " read -r -A array <<< "$(mycommand)"
|
||||
```
|
||||
|
||||
If the output should be a single element:
|
||||
|
||||
```sh
|
||||
array=( "$(mycommand)" )
|
||||
```
|
||||
|
||||
### Rationale:
|
||||
|
||||
You are doing unquoted command expansion in an array. This will invoke the shell's sloppy word splitting and glob expansion.
|
||||
|
||||
Instead, prefer explicitly splitting (or not splitting):
|
||||
|
||||
* If the command output should become a single array element, quote it.
|
||||
* If you want to split the output into lines or words, use `mapfile`, `read -ra` and/or `while` loops as appropriate.
|
||||
* If the command output should become a single array element, quote it.
|
||||
|
||||
This prevents the shell from doing unwanted splitting and glob expansion, and therefore avoiding problems with output containing spaces or special characters.
|
||||
|
||||
|
Reference in New Issue
Block a user