y
y
yq
Search…
Env Variable Operators
This operator is used to handle environment variables usage in path expressions. While environment variables can, of course, be passed in via your CLI with string interpolation, this often comes with complex quote escaping and can be tricky to write and read. Note that there are two forms, env which will parse the environment variable as a yaml (be it a map, array, string, number of boolean) and strenv which will always parse the argument as a string.

Read string environment variable

Running
1
myenv="cat meow" yq eval --null-input '.a = env(myenv)'
Copied!
will output
1
a: cat meow
Copied!

Read boolean environment variable

Running
1
myenv="true" yq eval --null-input '.a = env(myenv)'
Copied!
will output
1
a: true
Copied!

Read numeric environment variable

Running
1
myenv="12" yq eval --null-input '.a = env(myenv)'
Copied!
will output
1
a: 12
Copied!

Read yaml environment variable

Running
1
myenv="{b: fish}" yq eval --null-input '.a = env(myenv)'
Copied!
will output
1
a: {b: fish}
Copied!

Read boolean environment variable as a string

Running
1
myenv="true" yq eval --null-input '.a = strenv(myenv)'
Copied!
will output
1
a: "true"
Copied!

Read numeric environment variable as a string

Running
1
myenv="12" yq eval --null-input '.a = strenv(myenv)'
Copied!
will output
1
a: "12"
Copied!

Dynamic key lookup with environment variable

Given a sample.yml file of:
1
cat: meow
2
dog: woof
Copied!
then
1
myenv="cat" yq eval '.[env(myenv)]' sample.yml
Copied!
will output
1
meow
Copied!