How To Use The Bash Shell With Python's Subprocess Module Instead of - Bin - SH - SaltyCrane Blog
How To Use The Bash Shell With Python's Subprocess Module Instead of - Bin - SH - SaltyCrane Blog
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.
Solution thanks this great article: Working with Python subprocess - Shells, Processes, Streams, Pipes, Redirects and More
(http://jimmyg.org/blog/2009/working-with-python-subprocess.html)
import subprocess
def bash_command(cmd):
subprocess.Popen(cmd, shell=True, executable='/bin/bash')
Output:
For some reason, the above didn't work for my specific case, so I had to use the following instead:
import subprocess
def bash_command(cmd):
subprocess.Popen(['/bin/bash', '-c', cmd])
See also
Python subprocess.Popen documentation (http://docs.python.org/library/subprocess.html#subprocess.Popen)
Ubuntu documentation on differences between dash and bash (https://wiki.ubuntu.com/DashAsBinSh)
GNU Bash Reference Manual (http://www.gnu.org/software/bash/manual/html_node/index.html)
Related posts
How to capture stdout in real-time with Python (/blog/2009/10/how-capture-stdout-in-real-time-python/) — posted 2009-10-12
How to get stdout and stderr using Python's subprocess module (/blog/2008/09/how-get-stdout-and-stderr-using-python-
subprocess-module/) — posted 2008-09-23
How to use python and popen4 to capture stdout and stderr from a command (/blog/2007/03/how-to-use-python-and-popen4-
to-capture_12/) — posted 2007-03-12
Comments
#1 Osquarith commented on 2015-11-12:
disqus:2354674668
Write Preview
https://www.saltycrane.com/blog/2011/04/how-use-bash-shell-python-subprocess-instead-binsh/ 1/2
6/22/2020 How to use the bash shell with Python's subprocess module instead of /bin/sh - SaltyCrane Blog
Sign in to comment
(/blog/)
ABOUT (/ABOUT/)
I'm Eliot and this is my notepad for programming topics such as JavaScript, Python, Emacs, etc... more (/about/)
LINKS
Twitter Github Stack Overflow Atom Feed
(http://twitter.com/saltycrane)
(http://github.com/saltycrane)
(http://stackoverflow.com/users/101911/saltycrane)
(/feeds/latest/)
TAGS
c_cplusplus
blogproject cardstore (/blog/tag/c_cplusplus/)
aws (/blog/tag/aws/) (10) (/blog/tag/blogproject/) (20) (/blog/tag/cardstore/) (8) (12)
concurrency
colinux (/blog/tag/colinux/) (/blog/tag/concurrency/) conkeror
(2) (13) (/blog/tag/conkeror/) (2) css (/blog/tag/css/) (2)
cygwin (/blog/tag/cygwin/) django (/blog/tag/django/) emacs (/blog/tag/emacs/)
(17) (42) (25) git (/blog/tag/git/) (8)
hardware javascript keyboard
(/blog/tag/hardware/) (8) (/blog/tag/javascript/) (26) (/blog/tag/keyboard/) (10) mac (/blog/tag/mac/) (6)
preferences
matplotlib mercurial (/blog/tag/preferences/)
(/blog/tag/matplotlib/) (6) (/blog/tag/mercurial/) (4) nginx (/blog/tag/nginx/) (2) (10)
python (/blog/tag/python/) ratpoison regexes
pyqt (/blog/tag/pyqt/) (18) (168) (/blog/tag/ratpoison/) (3) (/blog/tag/regexes/) (6)
softwaretools
(/blog/tag/softwaretools/)
rsync (/blog/tag/rsync/) (3) (18) sql (/blog/tag/sql/) (16) ssh (/blog/tag/ssh/) (12)
subversion twisted (/blog/tag/twisted/) ubuntu (/blog/tag/ubuntu/)
(/blog/tag/subversion/) (6) (7) (68) urxvt (/blog/tag/urxvt/) (5)
vxworks
(/blog/tag/vxworks/) (25) wmii (/blog/tag/wmii/) (7)
ARCHIVE
2020 (/blog/2020/) (3) 2019 (/blog/2019/) (6) 2018 (/blog/2018/) (4) 2017 (/blog/2017/) (6)
2016 (/blog/2016/) (6) 2015 (/blog/2015/) (7) 2014 (/blog/2014/) (7) 2013 (/blog/2013/) (4)
2012 (/blog/2012/) (11) 2011 (/blog/2011/) (15) 2010 (/blog/2010/) (26) 2009 (/blog/2009/) (26)
2008 (/blog/2008/) (90) 2007 (/blog/2007/) (107) 2006 (/blog/2006/) (7)
https://www.saltycrane.com/blog/2011/04/how-use-bash-shell-python-subprocess-instead-binsh/ 2/2