Requirement:
In a BASH script,
...iterate over an array of environment variable names as shown below:
arr = ('env_var1' 'env_var2' 'env_var3')
and, using jq generate a JSON of environment variable name-value pairs like below:
{
"env_var1": "env_var1_value_is_1",
"env_var2": "env_var2_value_is_2",
"env_var3": "env_var3_value_is_3"
}
Current approach: Using this stackoverflow question's solution as a reference
printf '%s\n' "${arr[@]}" |
xargs -L 1 -I {} jq -sR --arg key {} '{ ($key): . }' | jq -s 'add'
where arr
array contains the environment variable names for which I want the values, however I am unable to interpolate the ${environment_variable_name}
into the JSON's value
in each key-value pair