Skip to content

Environment helpers: function to set environment variable? #149

@aprotyas

Description

@aprotyas

Feature request

It would be nice to have a way to set environment variables. I have at least one use-case in ros2/domain_bridge#60 (comment).

Feature description

A function that allows setting a named environment variable; essentially a dual of rcpputils::get_env_var.

Implementation considerations

It could be implemented exactly how rcpputils::get_env_var is implemented, by invoking the corresponding rcutils environment helper function.

One approach:

bool set_env_var(const char * env_var, const char * env_value)
{
  if (!rcutils_set_env(env_var, env_value)) {
    std::string err = rcutils_get_error_string().str;
    // Resetting the error state since error string has been extracted
    rcutils_reset_error();
    throw std::runtime_error(err);
  }
  return true;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions