2021-11-03 04:00:58 +00:00
# Env Variable Operators
2022-01-26 22:20:53 +00:00
These operators are used to handle environment variables usage in expressions and documents. 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.
There are three operators:
- `env` which takes a single environment variable name and parse the variable as a yaml node (be it a map, array, string, number of boolean)
- `strenv` which also takes a single environment variable name, and always parses the variable as a string.
- `envsubst` which you pipe strings into and it interpolates environment variables in strings using [envsubst ](https://github.com/a8m/envsubst ).
## Tip
To replace environment variables across all values in a document, `envsubst` can be used with the recursive descent operator
as follows:
```bash
2022-01-27 06:21:10 +00:00
yq '(.. | select(tag == "!!str")) |= envsubst' file.yaml
2022-01-26 22:20:53 +00:00
```
2021-11-03 04:00:58 +00:00
2022-02-06 03:39:46 +00:00
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified. 
`yq e <exp> <file>`
{% endhint %}
2021-11-03 04:00:58 +00:00
## Read string environment variable
Running
```bash
2022-01-27 06:21:10 +00:00
myenv="cat meow" yq --null-input '.a = env(myenv)'
2021-11-03 04:00:58 +00:00
```
will output
```yaml
a: cat meow
```
## Read boolean environment variable
Running
```bash
2022-01-27 06:21:10 +00:00
myenv="true" yq --null-input '.a = env(myenv)'
2021-11-03 04:00:58 +00:00
```
will output
```yaml
a: true
```
## Read numeric environment variable
Running
```bash
2022-01-27 06:21:10 +00:00
myenv="12" yq --null-input '.a = env(myenv)'
2021-11-03 04:00:58 +00:00
```
will output
```yaml
a: 12
```
## Read yaml environment variable
Running
```bash
2022-01-27 06:21:10 +00:00
myenv="{b: fish}" yq --null-input '.a = env(myenv)'
2021-11-03 04:00:58 +00:00
```
will output
```yaml
a: {b: fish}
```
## Read boolean environment variable as a string
Running
```bash
2022-01-27 06:21:10 +00:00
myenv="true" yq --null-input '.a = strenv(myenv)'
2021-11-03 04:00:58 +00:00
```
will output
```yaml
a: "true"
```
## Read numeric environment variable as a string
Running
```bash
2022-01-27 06:21:10 +00:00
myenv="12" yq --null-input '.a = strenv(myenv)'
2021-11-03 04:00:58 +00:00
```
will output
```yaml
a: "12"
```
2022-02-01 03:58:53 +00:00
## Dynamically update a path from an environment variable
2022-02-01 03:47:51 +00:00
The env variable can be any valid yq expression.
Given a sample.yml file of:
```yaml
a:
b:
- name: dog
- name: cat
```
then
```bash
2022-02-01 04:34:17 +00:00
pathEnv=".a.b[0].name" valueEnv="moo" yq 'eval(strenv(pathEnv)) = strenv(valueEnv)' sample.yml
2022-02-01 03:47:51 +00:00
```
will output
```yaml
2022-02-01 03:58:53 +00:00
a:
b:
- name: moo
- name: cat
2022-02-01 03:47:51 +00:00
```
2021-11-03 04:00:58 +00:00
## Dynamic key lookup with environment variable
Given a sample.yml file of:
```yaml
cat: meow
dog: woof
```
then
```bash
2022-01-27 06:21:10 +00:00
myenv="cat" yq '.[env(myenv)]' sample.yml
2021-11-03 04:00:58 +00:00
```
will output
```yaml
meow
```
2022-01-26 22:20:53 +00:00
## Replace strings with envsubst
Running
```bash
2022-01-27 06:21:10 +00:00
myenv="cat" yq --null-input '"the ${myenv} meows" | envsubst'
2022-01-26 22:20:53 +00:00
```
will output
```yaml
the cat meows
```
## Replace strings with envsubst, missing variables
Running
```bash
2022-01-27 06:21:10 +00:00
myenv="cat" yq --null-input '"the ${myenvnonexisting} meows" | envsubst'
2022-01-26 22:20:53 +00:00
```
will output
```yaml
the meows
```
## Replace strings with envsubst, missing variables with defaults
Running
```bash
2022-01-27 06:21:10 +00:00
myenv="cat" yq --null-input '"the ${myenvnonexisting-dog} meows" | envsubst'
2022-01-26 22:20:53 +00:00
```
will output
```yaml
the dog meows
```
## Replace string environment variable in document
Given a sample.yml file of:
```yaml
v: ${myenv}
```
then
```bash
2022-01-27 06:21:10 +00:00
myenv="cat meow" yq '.v |= envsubst' sample.yml
2022-01-26 22:20:53 +00:00
```
will output
```yaml
v: cat meow
```