getenv

Table of Contents

Prototype: getenv(variable, maxlength)

Return type: string

Description: Return the environment variable variable, truncated at maxlength characters

Returns an empty string if the environment variable is not defined. maxlength is used to avoid unexpectedly large return values, which could lead to security issues. Choose a reasonable value based on the environment variable you are querying.

Arguments:

  • variable: string, in the range: [a-zA-Z0-9_$(){}\[\].:]+
  • maxlength: int, in the range: 0,99999999999

Example:

body common control
{
      bundlesequence => { "example" };
}

bundle agent example
{
  vars:

      "myvar" string => getenv("EXAMPLE","2048");

  classes:

      "isdefined" not => strcmp("$(myvar)","");

  reports:

    isdefined::

      "The EXAMPLE environment variable is $(myvar)";

    !isdefined::

      "The environment variable EXAMPLE does not exist";

}

Output:

R: The EXAMPLE environment variable is getenv.cf

Notes:

History: This function was introduced in CFEngine version 3.0.4 (2010)