Updated SC2207 (markdown)

Vidar Holen
2018-03-07 14:17:35 -08:00
parent 01494d1e60
commit 74569a5b44

@@ -8,12 +8,6 @@ array=( $(mycommand) )
### Correct code: ### 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: If it outputs multiple lines, each of which should be an element:
```sh ```sh
@@ -34,14 +28,20 @@ IFS=" " read -r -a array <<< "$(mycommand)"
IFS=" " read -r -A array <<< "$(mycommand)" IFS=" " read -r -A array <<< "$(mycommand)"
``` ```
If the output should be a single element:
```sh
array=( "$(mycommand)" )
```
### Rationale: ### Rationale:
You are doing unquoted command expansion in an array. This will invoke the shell's sloppy word splitting and glob expansion. 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): 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 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. This prevents the shell from doing unwanted splitting and glob expansion, and therefore avoiding problems with output containing spaces or special characters.