So I'm trying to pass certain json elements as env vars and use them later on in a script.
Sample json:
JSON='{
"Element1": "file-123456",
"Element2": "Name, of, company written in, a very weird way",
"Element3": "path/to/some/file.txt",
}'
(part of the) script:
for s in $(echo $JSON | jq "{Element1,Element2,Element3} | to_entries|map(\"\(.key)=\(.value|tostring)\")|.[]");do
export $s
done
The problem I'm having is that Element2 is breaking the line when exporting but if I just run "echo $JSON | jq "{Element1,Element2,Element3} | to_entries|map(\"\(.key)=\(.value|tostring)\")|.[]"" at the cmd line, it looks fine:
"Element1=file-123456"
"Element2=Name, of, company written in, a very weird way"
"Element3=path/to/some/file.txt"
Actual output of script:
1.bash: line 100: export: `of,': not a valid identifier
1.bash: line 100: export: `company,': not a valid identifier
1.bash: line 100: export: `written,': not a valid identifier
As can be seen, the export command inserts single quotes into the "Element2" and I'm not sure where they come from.
The second variable (Element2) only contains the first element in the string (Name,). The behavior makes sense since the line is broken but for the life in me I cannot understand how or why the export command inserts them.
Any insight is appreciated, thank you!