From 5eb8832951808cf02115cd405c24b3f0a6537157 Mon Sep 17 00:00:00 2001 From: Jordan Date: Thu, 19 Apr 2018 01:58:48 +0000 Subject: [PATCH] Filling an array with var=( $(cmd) ) triggers SC2207; instructions updated accordingly from "correct code" on SC2207's wiki --- SC2046.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/SC2046.md b/SC2046.md index adca7ff..3fe5d93 100644 --- a/SC2046.md +++ b/SC2046.md @@ -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: