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?
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With