Skip to main content

Tool to Detect Surrounding Shell

Project description

Shellingham detects what shell the current Python executable is running in.


>>> import shellingham
>>> shellingham.detect_shell()
('bash', '/bin/bash')

detect_shell pokes around the process’s running environment to determine what shell it is run in. It returns a 2-tuple:

  • The shell name, always lowercased.
  • The command used to run the shell.

ShellDetectionFailure is raised if detect_shell fails to detect the surrounding shell.


  • The shell name is always lowercased.
  • On Windows, the shell name is the name of the executable, minus the file extension.
  • Currently the command only contains the executable name on Windows, even if the command is invoked by the full path. This may change in the future.

Notes for Application Developers

Remember, your application’s user is not necessarily using a shell. Shellingham raises ShellDetectionFailure if there is no shell to detect, but your application should almost never do this to your user.

A practical approach to this is to wrap detect_shell in a try block, and provide a sane default on failure:

    shell = shellingham.detect_shell()
except shellingham.ShellDetectionFailure:
    shell = provide_default()

There are a few choices for you to choose from.

  • The POSIX standard mandates the environment variable SHELL to refer to “the user’s preferred command language interpreter”. This is always available (even if the user is not in an interactive session), and likely the correct choice to launch an interactive sub-shell with.
  • A command sh is almost guarenteed to exist, likely at /bin/sh, since several POSIX tools rely on it. This should be suitable if you want to run a (possibly non-interactive) script.
  • All versions of DOS and Windows have an environment variable COMSPEC. This can always be used to launch a usable command prompt (e.g. cmd.exe on Windows).

Here’s a simple implementation to provide a default shell:

import os

def provide_default():
    if == 'posix':
        return os.environ['SHELL']
    elif == 'nt':
        return os.environ['COMSPEC']
    raise NotImplementedError(f'OS {!r} support not available')

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for shellingham, version 1.4.0
Filename, size File type Python version Upload date Hashes
Filename, size shellingham-1.4.0-py2.py3-none-any.whl (9.4 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size shellingham-1.4.0.tar.gz (9.6 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page