Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing environment variables set in R session from shell

Tags:

r

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?

like image 951
KirkD-CO Avatar asked Dec 18 '13 00:12

KirkD-CO


People also ask

Can be used to set environment variable in a shell which can be accessed from the sub shell?

Explanation: set command is used to display all the variables available in the current shell. set is a built-in command. env is an external command and runs in a child process. It thus displays only those variables that are inherited from its parent, the shell.

How do I see environment variables in terminal?

To display the values of environment variables, use the printenv command. If you specify the Name parameter, the system only prints the value associated with the variable you requested.

How do I see environment variables in bash?

Under bash shell: To list all the environment variables, use the command " env " (or " printenv "). You could also use " set " to list all the variables, including all local variables.


1 Answers

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
like image 174
Peyton Avatar answered Oct 01 '22 13:10

Peyton