The UnixEnv structure
Author: John Reppy
Keywords: Standard ML, SML, SML/NJ Library

The SML of NJ Library
The `UnixEnv` structure

The `UnixEnv` structure supports operations on the host process’s environment, which is essentially a list of strings of the form “`name=value`”, where the “=” character does not appear in `name`. We assume that environments are "well formed;" i.e., that an environment variable is only defined once.

Warning | Binding the user’s environment as an SML value and then exporting the SML heap image can result in incorrect behavior, since the environment bound in the heap image may differ from the user’s environment when the exported heap image is loaded.

## Synopsis

    signature UNIX_ENV
    structure UnixEnv : UNIX_ENV

## Interface

    val getFromEnv : (string * string list) -> string option

    val getValue : {name : string, default : string, env : string list} -> string

    val removeFromEnv : (string * string list) -> string list

    val addToEnv : (string * string list) -> string list

    val environ : unit -> string list

    val getEnv : string -> string option

## Description

`val getFromEnv : (string * string list) -> string option`

  `getEnv (name, env)` returns `SOME v` if `(name, v)` is in the environment `env`. Otherwise, it returns `NONE` if `name` is not bound in `env`.

`val getValue : {name : string, default : string, env : string list} -> string`

  `getEnv {name, default, env}` returns `v` if `(name, v)` is in the environment `env`. Otherwise, it returns `default` if `name` is not bound in `env`.

`val removeFromEnv : (string * string list) -> string list`

  `removeFromEnv (name, env)` removes any binding of `name` from the environment. Note that if `env` has multiple bindings of `name` (i.e., `env` is not well formed), then only the first binding is removed.

`val addToEnv : (string * string list) -> string list`

  `addToEnv (bind, env)` adds the binding `bind`, which should be of the form “`name=value`”, to the environment. If there was an existing binding of `name` in `env`, then it will be replaced.

`val environ : unit -> string list`

  `env ()` returns the user’s (host process) environment.

`val getEnv : string -> string option`

  `getEnv name` returns the binding of the environment variable `name` in the user’s (host process) environment.

## See Also

The Unix Library

SML/NJ Version 110.99.9 (November 4, 2025)
Last updated 2025-11-04 15:48:25 UTC