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

(require "asdf")
(defvar foo
(uiop:getenv "FOO")
"The contents of environment variable FOO.")

Common Lisp doesn't have a built-in way to examine environment variables, so we rely on the portability library UIOP. I believe all currently maintained implementations include UIOP as part of ASDF. The REQUIRE form loads ASDF, and thus UIOP.
If an environment variable is present and empty then FOO will be an empty string. "FOO" is not present on my system, so FOO gets NIL.
with Ada.Environment_Variables;
Foo : constant String :=
      Ada.Environment_Variables.Value (Name    => "FOO",
                                       Default => "none");

New implementation...
< >
tkoenig