Skip to main content

Tool to Detect Surrounding Shell

Project description

https://img.shields.io/pypi/v/shellingham.svg

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

Usage

>>> 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.

Notes

  • The shell name is always lowercased.

  • On Windows, the shell name is the name of the executable, minus the file extension.

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

try:
    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 guaranteed 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 os.name == 'posix':
        return os.environ['SHELL']
    elif os.name == 'nt':
        return os.environ['COMSPEC']
    raise NotImplementedError(f'OS {os.name!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.

Source Distribution

shellingham-1.5.4.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

shellingham-1.5.4-py2.py3-none-any.whl (9.8 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file shellingham-1.5.4.tar.gz.

File metadata

  • Download URL: shellingham-1.5.4.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for shellingham-1.5.4.tar.gz
Algorithm Hash digest
SHA256 8dbca0739d487e5bd35ab3ca4b36e11c4078f3a234bfce294b0a0291363404de
MD5 883d381da8fe788fb24428591e61eb20
BLAKE2b-256 58158b3609fd3830ef7b27b655beb4b4e9c62313a4e8da8c676e142cc210d58e

See more details on using hashes here.

File details

Details for the file shellingham-1.5.4-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for shellingham-1.5.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7ecfff8f2fd72616f7481040475a65b2bf8af90a56c89140852d1120324e8686
MD5 c7dfcd723fcc27c548ad3a7b06c976dc
BLAKE2b-256 e0f90595336914c5619e5f28a1fb793285925a8cd4b432c9da0a987836c7f822

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page