diff --git a/SC2207.md b/SC2207.md index 46428c5..2a67e21 100644 --- a/SC2207.md +++ b/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.