Issue
Here's the minimal example:
echo "{\"foo\": \"John\",\"bar\":\"Smith\"}" | jq -r '@sh "export FOO=\(.foo)\nexport BAR=\(.bar) \n"'
And in my real life scenario case that jq
selector is really long (think about 10 exports), how can I break them into multiple lines or something? I tried using \
as a separator at the end of the lines but it doesn't work unfortunately.
Solution
This would probably do:
. <(
jq -r '"export "+(to_entries|map((.key|ascii_upcase)+"="+(.value|@sh))|.[])'\
<<<'{"foo":"John","bar":"Smith"}'
)
Alternative with one export for all variables:
. <(
jq -r '
"export"+(
to_entries|
map(
" " +
(.key|ascii_upcase) +
"=" +
(.value|@sh)
) | add
)
' <<<'{"foo":"John","bar":"Smith"}'
)
You may also consider filtering-out replacing characters within .key
strings so they are always valid shell variable identifiers.
Answered By - Léa Gris Answer Checked By - Candace Johnson (WPSolving Volunteer)