GET_ENVIRONMENT_VARIABLE

 

Intrinsic Subroutine: Gets the value of an environment variable.

Syntax

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

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

Must be a scalar of type integer. If specified, its value is the length of the 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 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.

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".



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