Search code examples
rsyntax-errorsystem

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?


Solution

  • 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