KirkD-CO
KirkD-CO

Reputation: 1793

Accessing environment variables set in R session from shell

In my R session, I'm setting some environment variables using

Sys.setenv(BLAH="blah")

When I switch to the terminal (Ctrl-Z) and then try to access the environment variable, I see that it is not set.

echo $BLAH

The question is, where is the environment variable that I set from R, and if I want another process to see it, how can I get access to it?

Upvotes: 7

Views: 3976

Answers (1)

Peyton
Peyton

Reputation: 7396

The environmental variable dies with the process.

Each process has its own set of environmental variables, inherited from the parent process. When you create the environmental variable BLAH, you create it in the environment of the R process you're running, but not in the environment of the parent process.

If you want another process to access this environmental variable, you'll need to start the process from within R. Then the child process will inherit BLAH. This documentation for Sys.setenv mentions this:

Sys.setenv sets environment variables (for other processes called from within R or future calls to Sys.getenv from this R process).

For example:

Sys.setenv(BLAH="blah")
system("echo $BLAH")
# blah

Upvotes: 7

Related Questions