# GET_ENVIRONMENT_VARIABLE

Intrinsic Subroutine: Gets the value of an environment variable.

CALL GET_ENVIRONMENT_VARIABLE (name [, value, length, status, trim_name, errmsg])

name | (Input) Must be a scalar of type default character. It is the name of the environment variable.
value | (Output; optional) Must be a scalar of type default character. If specified, it is assigned the value of the environment variable specified by name. If the environment variable does not exist, value is assigned all blanks.
length | (Output; optional) Must be a scalar of type integer. If specified, its value is the length of the environment variable, if it exists; otherwise, length is set to 0.
status | (Output; optional) Must be a scalar of type integer. If specified, it is assigned a value of 0 if the environment variable exists and either has no value or its value is successfully assigned to value. It is assigned a value of -1 if the value argument is present and has a length less than the significant length of the environment variable value. It is assigned a value of 1 if the environment variable does not exist. For other error conditions, it is assigned a processor-dependent value greater than 2.
trim_name | (Input; optional) Must be a scalar of type logical. If the value is FALSE, then trailing blanks in name are considered significant. Otherwise, they are not considered part of the environment variable's name.
errmsg | (Input; output; optional) Must be a scalar of type default character. If an error occurs, it is assigned a processor-dependent explanatory message; otherwise, it is unchanged.

## Example

The following program asks for the name of an environment variable. If the environment variable exists in the program's environment, it prints out its value:

      program print_env_var
      character name*20, val*40
      integer len, status
      write (*,*) 'enter the name of the environment variable'
      read (*,*) name
      call get_environment_variable (name, val, len, status, .true.)
      if (status .ge. 2) then
         write (*,*) 'get_environment_variable failed: status = ', status
         stop
      end if
      if (status .eq. 1) then
         write (*,*) 'env var does not exist'
         stop
      end if
      if (status .eq. -1) then
         write (*,*) 'env var length = ', len, ' truncated to 40'
         len = 40
      end if
      if (len .eq. 0) then
         write (*,*) 'env var exists  but has no value'
         stop
      end if
      write (*,*) 'env var value = ', val (1:len)
      end

When the above program is invoked, the following line is displayed:

      enter the name of the environment variable

The following shows an example of what could be displayed if you enter "HOME".

- On a Linux* or macOS system:

        env var value = /home/our_space/usr4

- On a Windows* system:

        env var value = C:/

The following shows an example of what could be displayed if you enter "PATH".

- On a Linux or macOS system:

        env var length =          307  truncated to 40
        env var value = /site/our_space/usr4/progs/build_area

- On a Windows system:

        env var length =          829  truncated to 40
        env var value = C:\OUR_SPACE\BUILD_AREA\build_objects\

Parent topic: G