From pd@|gd @end|ng |rom gm@||@com  Tue Mar 19 10:51:36 2019
From: pd@|gd @end|ng |rom gm@||@com (peter dalgaard)
Date: Tue, 19 Mar 2019 10:51:36 +0100
Subject: [Rd] Possibly broken system2 env-option
In-Reply-To: <f38d1b55-c8f8-dbe4-94c4-bf15bb32c052@gmx.de>
References: <f38d1b55-c8f8-dbe4-94c4-bf15bb32c052@gmx.de>
Message-ID: <3244E2B3-3E44-4469-919B-4E5768F06F23@gmail.com>

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

pd$

Or even

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

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

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

So:

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

-pd

> On 18 Mar 2019, at 17:28 , Henning Bredel <h.bredel at 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 at 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 at cbs.dk  Priv: PDalgd at gmail.com



[Rd] Possibly broken system2 env-option
peter dalgaard