environment.get

Retrieves the value of the environment variable with the given name, or a default value if the variable doesn't exist.

Unlike environment.opIndex, this function never throws on Posix.

auto sh = environment.get("SHELL", "/bin/sh");

This function is also useful in checking for the existence of an environment variable.

auto myVar = environment.get("MYVAR");
if (myVar is null)
{
    // Environment variable doesn't exist.
    // Note that we have to use 'is' for the comparison, since
    // myVar == null is also true if the variable exists but is
    // empty.
}
class environment
static @safe
string
get
(
scope const(char)[] name
,
string defaultValue = null
)

Parameters

name const(char)[]

name of the environment variable to retrieve

defaultValue string

default value to return if the environment variable doesn't exist.

Return Value

Type: string

the value of the environment variable if found, otherwise null if the environment doesn't exist.

Throws

std.utf.UTFException if the variable contains invalid UTF-16 characters (Windows only).

Meta