Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the `shell` in `shell=True` in subprocess means `bash`?

I was wondering whether subprocess.call("if [ ! -d '{output}' ]; then mkdir -p {output}; fi",shell=True) will be interpreted by sh orzsh instead of bash in different server?

Anyone has ideas about this?

What should I do to make sure that it's interpreted by bash?

like image 218
Hanfei Sun Avatar asked Mar 16 '13 12:03

Hanfei Sun


People also ask

What does shell true mean in subprocess?

- from Actual meaning of 'shell=True' in subprocess. If args is a string, the string specifies the command to execute through the shell. This means that the string must be formatted exactly as it would be when typed at the shell prompt.

How can we avoid shell true in subprocess?

From the docs: args is required for all calls and should be a string, or a sequence of program arguments. Providing a sequence of arguments is generally preferred, as it allows the module to take care of any required escaping and quoting of arguments (e.g. to permit spaces in file names).

What shell does subprocess run use?

By default, running subprocess. Popen with shell=True uses /bin/sh as the shell. If you want to change the shell to /bin/bash , set the executable keyword argument to /bin/bash .

What is the difference between subprocess call and run?

I can say that you use subprocess. call() when you want the program to wait for the process to complete before moving onto the next process. In the case of subprocess. run() , the program will attempt to run all the processes at once, inevitably causing the program to crash.


1 Answers

http://docs.python.org/2/library/subprocess.html

On Unix with shell=True, the shell defaults to /bin/sh

Note that /bin/sh is often symlinked to something different, e.g. on ubuntu:

$ ls -la /bin/sh
lrwxrwxrwx 1 root root 4 Mar 29  2012 /bin/sh -> dash

You can use the executable argument to replace the default:

... If shell=True, on Unix the executable argument specifies a replacement shell for the default /bin/sh.

subprocess.call("if [ ! -d '{output}' ]; then mkdir -p {output}; fi",
                shell=True,
                executable="/bin/bash")
like image 75
Pavel Anossov Avatar answered Oct 12 '22 23:10

Pavel Anossov