Skip to content

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}/mydb

Circular 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 myapp

Database 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}/users

See Also

Released under the MIT License.