Environment Variables in Erlang
Our program demonstrates how to work with environment variables in Erlang. 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.
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 Erlang, we use the os
module to interact with environment variables. The os:putenv/2
function is used to set environment variables, while os:getenv/1
is used to retrieve them. The os:env/0
function returns all environment variables as a list of key-value tuples.
Note that in Erlang, strings are represented as lists of integers, so we use "~s"
in the format string of io:format/2
to print them as readable strings.