subprocess executing shell

Nobody nobody at nowhere.com
Thu Oct 22 21:31:53 CEST 2009


On Wed, 21 Oct 2009 11:24:37 -0400, Tim Arnold wrote:

> Hi, I'm writing a script to capture a command on the commandline and run it 
> on a remote server.
> I guess I don't understand subprocess because the code below exec's the 
> user's .cshrc file even though by default shell=False in the Popen call.

Using shell=True causes Popen to explicitly invoke the command via a
shell, i.e. "/bin/sh -c <command>" on Unix or "cmd /c <command>" on
Windows. If you use shell=False (the default), it uses fork()+exec() or
CreateProcess().

If the command which you are running ends up invoking a shell (as rsh
probably does), that's nothing to do with Popen.

In particular, if csh is being invoked, that isn't Popen's doing. It will
use either /bin/sh on Unix or %COMSPEC% (which is typically cmd.exe) on
Windows.

> Since the shell is executing in the child process anyway, is the only 
> difference when using shell=True is that environment variables can be 
> expanded in the command to be executed?

The difference is that the shell gets involved, so you can use environment
variables, backticks, redirections, pipelines, shell-builtins etc. It also
means that you need to quote or escape spaces and shell metacharacters
within arguments if you want them to be treated literally.




More information about the Python-list mailing list