Filling an array with var=( $(cmd) ) triggers SC2207; instructions updated accordingly from "correct code" on SC2207's wiki

Jordan
2018-04-19 01:58:48 +00:00
parent 137f08b803
commit 5eb8832951

@@ -31,7 +31,13 @@ In rare cases you actually want word splitting, such as in
This is because `pkg-config` outputs `-lssl -lcrypto`, which you want to break up by spaces into `-lssl` and `-lcrypto`. An alternative is to put the variables to an array and expand it:
args=( $(pkg-config --libs openssl) )
# For bash
mapfile -t args < <(pkg-config --libs openssl)
# For ksh
pkg-config --libs openssl | while IFS="" read -r line; do array+=("$line"); done
# expand args
gcc "${args[@]}" client.c
The power of using an array becomes evident when you want to combine, for example, the command result with user-provided arguments: