[Rd] Possibly broken system2 env-option

peter dalgaard pd@|gd @end|ng |rom gm@||@com
Tue Mar 19 10:51:36 CET 2019

You are using it wrong. It wants strings of the form "name=value", not a character vector with names as labels. So this is closer to the mark:

> system2("echo", env = c("VAR='Hello World'"), args = c("$VAR"))


However, as you see it doesn't work as intended. The problem is that the $-substitution refers to the environment of the shell executing the command. I.e. this does not work from a terminal command line either:

pd$ VAR="foo" echo $VAR


Or even

pd$ VAR="bar"
pd$ VAR="foo" echo $VAR

What you need is something like (NB: single quotes!)

pd$ VAR="foo" sh -c 'echo $VAR'


> system2("sh", env = c("VAR='Hello World'"), args = c("-c 'echo $VAR'"))
Hello World


> On 18 Mar 2019, at 17:28 , Henning Bredel <h.bredel using gmx.de> wrote:
> Hey all,
> what is wrong with this command:
>  system2("echo", env = c(VAR = "Hello World"), args = c("$VAR"))
> I am a bit confused, as help("system2") writes about the env option:
>> character vector of name=value strings to set environment variables.
> Is this option buggy, or am I using it just wrong?
> Thanks for your help
>  Henning
> ______________________________________________
> R-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes using cbs.dk  Priv: PDalgd using gmail.com

More information about the R-devel mailing list