Fortran Wiki


Get the value of the environmental variable name.


Fortran 2003 and later




call get_environment_variable(name[, value, length, status, trim_name)


  • name - Shall be a scalar of type character(1).
  • value - Shall be a scalar of type character(1).
  • length - Shall be a scalar of type integer(4).
  • status - Shall be a scalar of type integer(4).
  • trim_name - Shall be a scalar of type logical(4).

Return value

Stores the value of name in value. If value is not large enough to hold the data, it is truncated. If name is not set, value will be filled with blanks. Argument length contains the length needed for storing the environment variable name or zero if it is not present. status is -1 if value is present but too short for the environment variable; it is 1 if the environment variable does not exist and 2 if the processor does not support environment variables; in all other cases status is zero. If trim_name is present with the value .false., the trailing blanks in name are significant; otherwise they are not part of the environment variable name.


program test_getenv
  character(len=255) :: homedir
  call get_environment_variable("home", homedir)
  write (*,*) trim(homedir)
end program

category: intrinsics