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.
Running
myenv="cat meow" yq eval --null-input '.a = env(myenv)'
will output
a: cat meow
Running
myenv="true" yq eval --null-input '.a = env(myenv)'
will output
a: true
Running
myenv="12" yq eval --null-input '.a = env(myenv)'
will output
a: 12
Running
myenv="{b: fish}" yq eval --null-input '.a = env(myenv)'
will output
a: {b: fish}
Running
myenv="true" yq eval --null-input '.a = strenv(myenv)'
will output
a: "true"
Running
myenv="12" yq eval --null-input '.a = strenv(myenv)'
will output
a: "12"
Given a sample.yml file of:
cat: meowdog: woof
then
myenv="cat" yq eval '.[env(myenv)]' sample.yml
will output
meow