Interpolation
env.zig supports ${VAR} syntax to reference other environment variables.
Enable Interpolation
zig
var env = env_mod.Env.init(allocator, .{
.interpolate = true,
});Syntax
Reference another variable with ${VARIABLE_NAME}:
env
DATABASE_HOST=localhost
DATABASE_PORT=5432
DATABASE_URL=postgres://${DATABASE_HOST}:${DATABASE_PORT}/mydbCircular Detection
env.zig detects circular references and prevents infinite loops:
zig
// This will error with a circular reference detected
try env.parseString(
\\A=${B}
\\B=${A}
\\
);Examples
Basic Interpolation
env
APP_NAME=myapp
GREETING=hello
MESSAGE=${GREETING} from ${APP_NAME}zig
try stdout.print("MESSAGE = {s}\n", .{env.get("MESSAGE").?});
// Output: MESSAGE = hello from myappDatabase URL Construction
env
DB_HOST=localhost
DB_PORT=5432
DB_NAME=mydb
DATABASE_URL=postgres://${DB_HOST}:${DB_PORT}/${DB_NAME}Nested Interpolation
env
BASE_URL=https://api.example.com
VERSION=v1
API_ENDPOINT=${BASE_URL}/${VERSION}/usersSee Also
- Getting Started for configuration options
- Configuration for all config options
- API Reference for the full API
