Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #205 Get an environment variable

Read an environment variable with the name "FOO" and assign it to the string variable foo. If it does not exist or if the system does not support environment variables, assign a value of "none".

import "cmp"
import "os"
foo := cmp.Or(os.Getenv("FOO"), "none")
import "os"
foo, ok := os.LookupEnv("FOO")
if !ok {
	foo = "none"
}
import "os"
foo := os.Getenv("FOO")
if foo == "" {
	foo = "none"
}

This is fine if empty string means "no value" in your use case.

To distinguish between an empty value and an unset value, use os.LookupEnv.
with Ada.Environment_Variables;
Foo : constant String :=
      Ada.Environment_Variables.Value (Name    => "FOO",
                                       Default => "none");

New implementation...
< >
tkoenig