Comment on page
Eval
Use
eval
to dynamically process an expression - for instance from an environment variable.eval
takes a single argument, and evaluates that as a yq
expression. Any valid expression can be used, be it a path .a.b.c | select(. == "cat")
, or an update .a.b.c = "gogo"
.Tip: This can be a useful way to parameterise complex scripts.
Given a sample.yml file of:
pathExp: .a.b[] | select(.name == "cat")
a:
b:
- name: dog
- name: cat
then
yq 'eval(.pathExp)' sample.yml
will output
name: cat
The env variable can be any valid yq expression.
Given a sample.yml file of:
a:
b:
- name: dog
- name: cat
then
pathEnv=".a.b[0].name" valueEnv="moo" yq 'eval(strenv(pathEnv)) = strenv(valueEnv)' sample.yml
will output
a:
b:
- name: moo
- name: cat
Last modified 8mo ago