dertomtom
dertomtom

Reputation: 189

How to prevent R system command syntax error

I am trying to run a command similar to

> system("cat <(echo $PATH)")

which fails when run from within R or Rstudio with the following error message:

sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `cat <(echo $PATH)'

However, if I run this on the command line it works fine:

$ cat <(echo $PATH)
[...]/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin

I checked that the shell I am using is bash using system("echo $SHELL"). Can anyone help me solving this?

Upvotes: 1

Views: 255

Answers (1)

Paul
Paul

Reputation: 9107

This syntax works in bash but not sh. The $SHELL environment variable doesn't necessarily mean that is the shell being used. echo $0 will show your shell.

system("echo $0")
#> sh

You could force bash to be used like this

system("bash -c 'cat <(echo $PATH)'")
#> /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin

Upvotes: 2

Related Questions