mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Filling an array with var=( $(cmd) ) triggers SC2207; instructions updated accordingly from "correct code" on SC2207's wiki
@@ -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:
|
||||
|
Reference in New Issue
Block a user