getenv and setenv subroutines in Fortran

getenv and setenv subroutines in Fortran

Does the Intel Fortran compiler have Fortran wrappers for getenv and setenv subroutines? If so, which link flag should I use to obtain them?

8 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

If you consult the documentation which comes with ifort, you will see that getenv requires USE IFPORT (which doesn't involve any link options).  Likewise, getenvqq and setenvqq may be looked up via ifport library overview.

There is also the standard compliant (as of F2003) intrinsic subroutine `get_environment_variable(…)` but there is no standard compliant means of setting an environment variable, as far as I know.

call execute_command_line would at least permit setting environment variables using different Fortran compilers running under similar OS shells.  I think several people filed requests for this feature since f2008 standard came out, and a DPD issue number DPD200137238 was quoted on this forum over year ago.  My premier issue 628820 is 4 years old. Primary alternative Fortrans have supported it for 2 years or more.

I would very much like to see this added to the standard.


The call execute_command_line would at least permit setting environment variables does work when the command line is executed inside the same shell context as the Fortran program. It would be open to the implementation as to if the execute_command_line spawned a new command shell, inheriting current environment on entry, discarding on exit. IOW, while this works today, it may not work some time later.

Jim Dempsey

If the Fortran processor implements getenv via a mechanism like C's char **envp, setenv would not necessarily work as seen by the Fortran program because it might only be looking at the state of the environmental variables at the time the program loaded, rather than the state current when getenv is invoked. This is the case in gfortran at least, when changes made by setting environmental variables via API calls at runtime are invisible to getenv.

Fortran has the standard intrinsic GET_ENVIRONMENT_VARIABLE, which is getenv. Intel Fortran supports this. There is no corresponding intrinsic for setenv. (But as noted above we support both setenv and getenv with USE IFPORT.)

Note that EXECUTE_COMMAND_LINE won't help you as any changes made in that context are local to the spawned process.

Retired 12/31/2016

Leave a Comment

Please sign in to add a comment. Not a member? Join today