y
y
yq
Search…
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, beit a path .a.b.c | select(. == "cat"), or an update .a.b.c = "gogo".
Tip: This can be useful way parameterise complex scripts.
Note that versions prior to 4.18 require the 'eval/e' command to be specified.
yq e <exp> <file>

Dynamically evaluate a path

Given a sample.yml file of:
1
pathExp: .a.b[] | select(.name == "cat")
2
a:
3
b:
4
- name: dog
5
- name: cat
Copied!
then
1
yq 'eval(.pathExp)' sample.yml
Copied!
will output
1
name: cat
Copied!

Dynamically update a path from an environment variable

The env variable can be any valid yq expression.
Given a sample.yml file of:
1
a:
2
b:
3
- name: dog
4
- name: cat
Copied!
then
1
pathEnv=".a.b[0].name" valueEnv="moo" yq 'eval(strenv(pathEnv)) = strenv(valueEnv)' sample.yml
Copied!
will output
1
a:
2
b:
3
- name: moo
4
- name: cat
Copied!