environment.get

string get(scope const(char)[] name, string defaultValue = null) @safe

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.
}

Parameters

namename of the environment variable to retrieve
defaultValuedefault value to return if the environment variable doesn't exist.

Returns

the value of the environment variable if found, otherwise

null if the environment doesn't exist.

Throws

UTFException if the variable contains invalid UTF-16

characters (Windows only).