Our first program will demonstrate how to work with environment variables in Fortran. Environment variables are a universal mechanism for conveying configuration information to 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.
Note that Fortran doesn’t have built-in functions for setting environment variables, so we use the C library function setenv through an interface. The get_environment_variable intrinsic function is used to retrieve environment variable values and list all environment variables.