system2("bash", "ls") -> cannot execute binary file

543 views Asked by At

Anyone got an idea why

system2("bash", "ls") 

would result in (both in R-Gui and RStudio Console)

/usr/bin/ls: /usr/bin/ls: cannot execute binary file 

while other shells work w/o issues (see example below) and the "bash" in the RStudio Terminal also works w/o problems. AND if there is a remedy for that?

Working system2 example:

system2("powershell", "dir") 

Running R-3.6.3 on Windows 10 here ... with "Git bash" executing as "bash".

PS: just in case someone wonders - I also tried the full path ie /usr/bin/ls -> same result

1

There are 1 answers

2
GWD On BEST ANSWER

Ok - this does the trick

system2("bash", input = "ls")

so instead of args = ... as with the powershell command one needs (or more interestingly sometimes 'can') use the input = ... parameter with bash

OR as mentioned in the comments by @oguzismail this will also work

system2("bash", "-c ls") 

as well as pointed out by @Christoph

system2("ls") 

also works sometimes (ie in some RStudio[?] setups it will not return results in some it will).

But I have come to notice that different combinations of R-versions with different RStudio versions [or more probably Locales] will not always behave consistently of course also depending on the availability of bash commands like ls (more generally /usr/bin) being on the PATH or not.

So choose what suits u best!