0% found this document useful (0 votes)
80 views

How To Use The Bash Shell With Python's Subprocess Module Instead of - Bin - SH - SaltyCrane Blog

This document discusses how to use the bash shell instead of the default /bin/sh shell when using the Python subprocess module. By default, subprocess uses /bin/sh, but it can be configured to use /bin/bash instead by setting the executable keyword argument to "/bin/bash". As an alternative, "/usr/bin/env bash" can also be used. The document provides an example of running a bash command using the subprocess module and overriding the shell.

Uploaded by

Hbsodndk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views

How To Use The Bash Shell With Python's Subprocess Module Instead of - Bin - SH - SaltyCrane Blog

This document discusses how to use the bash shell instead of the default /bin/sh shell when using the Python subprocess module. By default, subprocess uses /bin/sh, but it can be configured to use /bin/bash instead by setting the executable keyword argument to "/bin/bash". As an alternative, "/usr/bin/env bash" can also be used. The document provides an example of running a bash command using the subprocess module and overriding the shell.

Uploaded by

Hbsodndk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

6/22/2020 How to use the bash shell with Python's subprocess module instead of /bin/sh - SaltyCrane Blog

SALTYCRANE (/) BLOG (/BLOG/) — Notes on JavaScript and web development

How to use the bash shell with Python's


subprocess module instead of /bin/sh
Date: 2011 (/blog/2011/)-04-13  |  Modified: 2011-04-16  |  Tags: processes (/blog/tag/processes/), python
(/blog/tag/python/)  |  1 Comment (/blog/2011/04/how-use-bash-shell-python-subprocess-instead-
binsh/#comments)

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')

bash_command('a="Apples and oranges" && echo "${a/oranges/grapes}"')

Output:

Apples and grapes

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:

Can /usr/bin/env bash be used?

disqus:2354674668

0 Comments - powered by utteranc.es

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

Styling with Markdown is supported 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)

Built with Nginx (https://nginx.org/en/), Django (https://www.djangoproject.com/), and Bootstrap


(https://getbootstrap.com/) | Hosted by Linode (https://www.linode.com/)

https://www.saltycrane.com/blog/2011/04/how-use-bash-shell-python-subprocess-instead-binsh/ 2/2

You might also like