16

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?

Hanfei Sun
  • 45,281
  • 39
  • 129
  • 237

3 Answers3

36

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")
Pavel Anossov
  • 60,842
  • 14
  • 151
  • 124
4

To specify the shell, use the executable parameter with shell=True:

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

In [26]: subprocess.call("if [ ! -d '{output}' ]; then mkdir -p {output}; fi", shell=True, executable='/bin/bash')
Out[26]: 0

Clearly, using the executable parameter is cleaner, but it is also possible to call bash from sh:

In [27]: subprocess.call('''bash -c "if [ ! -d '{output}' ]; then mkdir -p {output}; fi"''', shell=True)
Out[27]: 0
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
3

You can explicitly invoke the shell of your choice, but for the example code you posted this is not the best approach. Instead, just write the code in Python directly. See here: mkdir -p functionality in Python

Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436