I'm tearing my hair out a bit at this. I'm trying to write a shell script which indexes through all environment variables, and then overrides variables in a config.json file.
I have a demo angular app which is running in a docker file and I'd like to be able to set config variables as environment variables. I know it's not the safest approach but this code will never see production and the flexibility it grants me is desirable.
So far I have this:
#!/bin/sh
env | while IFS= read -r line; do
value=${line#*=}
name=${line%%=*}
jq --arg name "$name" --arg value "$value" '.[$name] = $value' src/assets/config.json > src/assets/config.json
done
jq . src/assets/config.json
The output json file keeps ending up empty though.
JQ has a built-in
envfunction that refers to the current environment and exposes it as a dictionary.Thus, all you need is:
...or, to merge an existing config file to add new keys from the environment (and overwrite existing keys where they conflict):