Os.env
func Os.env(name: string): string | nullOs.env returns the value of the environment variable name, or null when it is not set; a variable set empty gives "". Names are matched as the host matches them: exactly on Unix, and ignoring case on Windows. An empty name, or one holding =, names no variable and gives null.
Details
nullmeans the variable is not set. A variable set to the empty string gives"".- Names match as the host matches them: exactly on Unix, so
pathis notPATH, and ignoring case on Windows. An empty name, or one holding=, names no variable and givesnull. Os.envreads the program's own environment. Theenvof a Cmd sets a child's environment and does not change whatOs.envreturns.
Basic examples
A variable that is not set gives null:
println(Os.env("APP_NO_SUCH_VARIABLE"))
println(Os.env("PATH") != null)null
trueA variable that is set gives its value, and one set to the empty string gives "", not null. Here the program runs as APP_MODE=debug APP_EMPTY= ant run main.ant:
println(Os.env("APP_MODE"))
println(Os.env("APP_EMPTY") == "")
println(Os.env("APP_NO_SUCH_VARIABLE"))debug
true
nullThe value of a variable the host sets depends on the machine, such as /Users/ann for HOME:
println(Os.env("HOME"))Scope
The result is string | null, so give a default with ?? or narrow it before using it as a string:
let editor = Os.env("APP_EDITOR") ?? "vi"
println(editor)
let path = Os.env("PATH")
if path != null {
println(path.isEmpty())
}vi
falseOptional chaining parses a value only when there is one:
let port = Os.env("APP_PORT")?.toInt() ?? 8080
println(port)8080Applications
Read a setting from the environment, default it when it is missing, and reject it when it is malformed:
func parsePort(text: string | null): Result[int, Error] {
if text == null { return Result.ok(8080) }
let port = text.toInt()
if port == null || port < 1 || port > 65535 {
return Result.fail("APP_PORT: want a port from 1 to 65535, got ${text}")
}
Result.ok(port)
}
println(parsePort(Os.env("APP_PORT")))
println(parsePort("3000"))
println(parsePort("http"))Ok{ value: 8080 }
Ok{ value: 3000 }
Err{ error: Error{ message: "APP_PORT: want a port from 1 to 65535, got http" } }Require a variable, and treat an empty value as missing:
func required(name: string): Result[string, Error] {
let value = Os.env(name)
if value == null || value.isEmpty() {
return Result.fail("${name} is not set")
}
Result.ok(value)
}
println(required("PATH").isOk())
println(required("APP_DATABASE_URL"))true
Err{ error: Error{ message: "APP_DATABASE_URL is not set" } }Give a child a small environment of its own, with the program's PATH and one variable more:
let child = Cmd.new(
"sh",
["-c", "echo $GREETING"],
env: Map{ "PATH": Os.env("PATH") ?? "", "GREETING": "hello" },
)
println(try Cmd.read(child))helloProperties and relations
Os.env agrees with Os.environ, which holds every variable that is set:
let env = Os.environ()
for e in env.entries() {
assert(Os.env(e.key) == e.value)
}
assert(env["APP_NO_SUCH_VARIABLE"] == null)
assert(Os.env("APP_NO_SUCH_VARIABLE") == null)Possible issues
The result may be null, so it is not a string:
let home: string = Os.env("HOME") // error: cannot use string | null where string is expectedGive a default, or handle the missing case:
let home: string = Os.env("HOME") ?? "/"
println(home.isEmpty())falseSee also
Os.environ · Os.home · Os.which · Os.args · Cmd.new · ?? · ?.
