Environment Variables in Lisp
Our program demonstrates how to work with environment variables in Lisp. Environment variables are a universal mechanism for conveying configuration information to Unix programs. Let’s look at how to set, get, and list environment variables.
To run the program, save it as environment-variables.lisp
and use your Lisp interpreter. For example, if you’re using SBCL:
Running the program shows that we pick up the value for FOO
that we set in the program, but that BAR
is NIL (equivalent to empty string in this context).
The list of keys in the environment will depend on your particular machine.
If we set BAR
in the environment first, the running program picks that value up:
Note: This example uses SBCL-specific functions (sb-posix:setenv
, sb-posix:getenv
, sb-ext:posix-environ
). If you’re using a different Lisp implementation, you might need to use different functions or load additional libraries to work with environment variables.