Skip to content
Latchkey

yq env and strenv: Inject Environment Variables

yq strenv(VAR) reads an environment variable as a string; env(VAR) reads it with YAML type inference.

Pipelines pass values through the environment, and yq pulls them in with env() or strenv(). The choice matters: env() can turn a version like 1.10 into the float 1.1, which strenv() avoids.

What it does

strenv(VAR) substitutes the environment variable VAR as a string, always. env(VAR) substitutes it but applies YAML type inference, so "true", "3", and "1.10" become a boolean, an int, and a float. For image tags and versions, strenv is almost always what you want.

Common usage

Terminal
TAG=1.10 yq '.image.tag = strenv(TAG)' values.yaml   # stays "1.10"
N=3 yq '.replicas = env(N)' values.yaml                # becomes int 3
yq -i '.metadata.labels.sha = strenv(GITHUB_SHA)' deploy.yaml

env vs strenv

FunctionResult type
strenv(VAR)Always a string
env(VAR)YAML-inferred (int/float/bool/string)
env(VAR)"1.10" becomes float 1.1 (loses trailing zero)
strenv(VAR)"1.10" stays the string "1.10"
(env(X)) as $x | ...Bind an env value to a variable

In CI

Bumping a k8s image tag from a pipeline variable: IMAGE="$REGISTRY/app:$SHA" yq -i '.spec.template.spec.containers[0].image = strenv(IMAGE)' deploy.yaml. Use strenv so a numeric-looking tag is not reinterpreted as a number.

Common errors in CI

A tag written as 1.1 instead of "1.10" means env() coerced it to a float; switch to strenv(). "Error: ... variable VAR not defined" style empty output means the env var was not exported into yq's environment; prefix it on the same command line. Note Python yq does not have strenv/env at all; it reads $ENV via jq's env.VAR, so these functions erroring confirms you are on a different yq.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →