[R] Problem with system() and source on linux

Ivan Krylov krylov@r00t @ending from gm@il@com
Thu Dec 20 16:19:47 CET 2018

On Thu, 20 Dec 2018 12:00:04 +0100
Agustin Lobo <aloboaleu using gmail.com> wrote:

> For a given program, I need to set up its environment, which I
> normally do with source /home/alobo/OTB-6.6.0-Linux64/otbenv.profile
> from the terminal.

The problem with this approach is that in Unix-like systems, child
processes cannot directly modify environment variables of their
parents (or any other processes besides those they are about to spawn).
In /bin/bash, `source` is a special builtin command that runs without
spawning child processes - therefore being able to modify the
environment of the *current* process.

> Now, when I try to do the same from within R, I get:
> > system("source /home/alobo/OTB-6.6.0-Linux64/otbenv.profile",
> > intern=TRUE)  
> sh: 1: source: not found

Moreover, `source` is a bash-ism and R launches the `system` commands
using /bin/sh, which might be different from /bin/bash. The
POSIX-correct way to include a batch file in the current shell session
is a dot: `. /home/alobo/OTB-6.6.0-Linux64/otbenv.profile`

Since this way you can only modify the environment of the temporary
shell process spawned by `system`, you have set up the environment and
launch the executable in a single `system` command:

system(". /home/alobo/OTB-6.6.0-Linux64/otbenv.profile; exec

Best regards,

More information about the R-help mailing list