Environment Variables in Clojure
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 in Clojure.
Running the program shows that we pick up the value for FOO
that we set in the program, but that BAR
is empty.
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.
In Clojure, we use System/setProperty
to set environment variables within the program, and System/getenv
to retrieve them. The System/getenv
function returns a map of all environment variables when called without arguments, which we can use to list all variables.
Note that setting environment variables within a Clojure program using System/setProperty
only affects the current JVM instance and doesn’t modify the system-wide environment variables. For persistent changes, you would need to set the variables at the system or shell level before running the Clojure program.