Modules

Environment

moduleextensionSource

Available on macOS, Linux, Windows, Wasm·since 0.31.0

#import "Extensions/Environment";

Reading the variables the program was started with.

The values are text in the environment as the system hands it over; this module only reads. Setting variables is not part of it.

Example
assert(has_environment_variable("PATH"));
assert(!has_environment_variable("JAI_DOC_SURELY_UNSET_VARIABLE"));

Doesn't run in the browser (skipped on wasm).

Procedures#

(name: string) -> string

The value of the environment variable name, in temporary storage, or "" when it is not set. On Windows the name and value go through UTF-16, so non-ASCII values come back as UTF-8.

assert(environment_variable("PATH").count > 0); // set on every desktop platform
assert(environment_variable("JAI_DOC_SURELY_UNSET_VARIABLE") == "");

kept := copy_string(environment_variable("PATH"));
defer free(kept);
assert(kept.count > 0);

Doesn't run in the browser (skipped on wasm).

(name: string) -> bool

Whether name is set at all, which environment_variable does not say for a variable set to the empty string.