Skip to content

Feature: interpolate env. expressions #3397

@jakub-bochenski

Description

@jakub-bochenski

As a debugging aide it would be nice if jq would have a flag to just print a given expression with env.|ENV. statements interpolated.

e.g.

$ echo $SHELL
/bin/bash
$ jq -n '{ foo: env.SHELL }'
{
  "foo": "/bin/bash"
}
$ jq --interpolate-only '{ foo: env.SHELL }'
{ foo: "/bin/bash" }

I wrote a small script to do this, but if jq did that one could be sure the logic is correct.
(e.g. this one only works for env. and not ENV. :))

interpolate_jq_expression() {
  local expression="$1"
  local value var

  for var in $(echo "$expression" | grep -o 'env\.[A-Za-z_][A-Za-z0-9_]*' | sed 's/env\.//')
  do
    value=$(jq -R <<< "${!var}")
    expression=$(echo "$expression" | sed "s/env\.$var/$value/g")
  done

  echo "$expression"
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions