In R, When I run
system("FOO='test123'")
I would expect
system("echo $FOO")
to return
test123
in the same way that
system("echo $USER")
returns my username
But it returns nothing. Why is this?
Why would anyone want to do this? I was trying to simulate the use of env FOO='test1234 R -vanilla < script.R while writing script.R, which in turn calls system("echo $FOO)`
I don’t know R, but in other languages
system()(at least on Unix-like systems) creates a new shell (/bin/sh) process to execute the command. YourFOO='test123'sets the value of$FOO, but only within that process. Yoursystem("echo $FOO")executes in a new process in which$FOOhasn’t been set.If R has a way to set environment variables internally (
setenv, perhaps?), you should use that instead.EDIT: As @Joshua says in a comment, it’s
Sys.setenv.