Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What shell does Python's subprocess use?

This question and answer demonstrates how to use Python's subprocess module to interact with bash from Python.

So if subprocess doesn't use the system's default shell, then what shell does it use to run commands like this:

    import subprocess

    print subprocess.check_output(["ls", "-la"])
like image 220
Sam Malayek Avatar asked Aug 06 '26 06:08

Sam Malayek


1 Answers

If shell is not passed as a keyword argument it uses fork_exec, or _winapi.CreateProceess

  • https://github.com/python/cpython/blob/f3751efb5c8b53b37efbbf75d9422c1d11c01646/Modules/_posixsubprocess.c#L600
  • https://github.com/python/cpython/blob/f3751efb5c8b53b37efbbf75d9422c1d11c01646/Modules/_winapi.c#L1062
  • https://github.com/python/cpython/blob/e02ab59fdffa0bb841182c30ef1355c89578d945/Lib/subprocess.py#L1770
  • https://github.com/python/cpython/blob/e02ab59fdffa0bb841182c30ef1355c89578d945/Lib/subprocess.py#L1434

If shell=True:

  • posix it uses /bin/sh, .
  • windows it uses cmd.exe.

See:

  • https://github.com/python/cpython/blob/master/Lib/subprocess.py#L1698-L1704
  • https://github.com/python/cpython/blob/e02ab59fdffa0bb841182c30ef1355c89578d945/Lib/subprocess.py#L1421-L1425
  • https://github.com/python/cpython/blob/e02ab59fdffa0bb841182c30ef1355c89578d945/Lib/subprocess.py#L1670
like image 91
jmunsch Avatar answered Aug 08 '26 22:08

jmunsch