0

I'm trying to edit a config file with jq with some environment variables, but I'm getting stuck on the double quote escaping and formatting.

For example...

Inside test.conf is:

{
    "log_level":"INFO"
}

I can edit the file with a new hardcoded value without issue by running:

jq -e '.station_conf.log_level="WARN"' test.conf > test.conf.tmp && cp test.conf.tmp test.conf

Which results in test.conf:

{
    "log_level":"WARN"
}

But trying to call the env variable:

jq -e '.station_conf.log_level="$LOG_LEVEL"' test.conf > test.conf.tmp && cp test.conf.tmp test.conf

Results in:

{
    "log_level":"$LOG_LEVEL"
}

Which of course is because of the double quotes in the jq command. How do I escape them to let the variable through, but still end up with properly formatted json with the double quotes around the value?

Don T Spamme
  • 199
  • 7
  • In case anyone was curious I managed to get it to work with `jq -e '.station_conf.log_level="'$LOG_LEVEL'"' test.conf > test.conf.tmp && cp test.conf.tmp test.conf`. – Don T Spamme Apr 19 '21 at 14:09
  • 1
    The solution in the comments is horrible. Don't generate code from the shell. Use `jq --arg log_level "$LOG_LEVEL '... = $log_level'` or `LOG_LEVEL="$LOG_LEVEL" jq '... = $ENV.LOG_LEVEL'` – ikegami Apr 20 '21 at 00:46
  • @Inian Actually a duplicate of [this](https://stackoverflow.com/q/40027395/589924). Can you add to the list of duplicates, please? – ikegami Apr 20 '21 at 00:52

0 Answers0