Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between system() and shell() [duplicate]

Tags:

windows

r

On R for Windows, there are two functions to invoke system commands. On the one hand it is possible to use system() (or the newer system2() function) to execute system commands. On the other hand, it is possible to use shell(). In my opinion they both should do the same. What would be the difference between them?

From reading the documentation, shell is described as a more user-friendly wrapper around system. I can see that both have slightly different arguments, but I don't really see the point of calling shell more user-friendly. Except for this, I cannot see any other differences and even executing some basic code gives the same results (except for the quotes).

> system('ECHO "test"')
test
> shell('ECHO "test"')
"test"

What would be the argument to use one rather than the other?

like image 426
takje Avatar asked Jan 23 '17 13:01

takje


People also ask

How is system different from Execv?

The system( ) function works differently from the exec*( ) functions; instead of replacing the currently executing program, it creates a new process with fork( ) . The new process executes the shell with execve( ) while the original process waits for the new process to terminate.

What is the difference between system and exec?

system() will execute the supplied command in a child process that it spawns. exec() will replace the current process with the invocation of the new executable that you specify. If you want to spawn a child process using exec , you'll have to fork() your process beforehand.


1 Answers

If your system commands are accepted by CMD.EXE (default on Windows), there isn't much difference. However if you want to use different shells like sh to execute non-CMD.exe scripts, it can save you having to prefix every system command. It will also help with related issues such as having to switch out every / for \.

like image 191
mpjdem Avatar answered Sep 21 '22 06:09

mpjdem