Shellingham Documentation

repository·master·Indexed 43 days ago

https://github.com/sarugaku/shellingham

A Python tool used to detect the shell environment in which the current Python executable is running. It provides the `detect_shell()` function to identify the shell type and executable path, and includes the `ShellDetectionFailure` exception for handling cases where the shell environment cannot be determined.

Tokens
712
Snippets
3
Records
4
Agent score
6%

What's inside Shellingham

  1. Handle ShellDetectionFailure in applications

    master

    When using detect_shell(), your application should anticipate that a shell might not be present (e.g., if the user is running a non-interactive process). Instead of letting shellingham.ShellDetectionFailure crash your program, wrap the call in a try...except block and provide a fallback shell.

    Recommended fallback strategies:

    • POSIX: Use the SHELL environment variable (the user's preferred command language interpreter).
    • POSIX (Scripting): Use sh (usually /bin/sh) for running scripts.
    • Windows: Use the COMSPEC environment variable (e.g., cmd.exe).
    import os
    import shellingham
    
    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')
    
    try:
        shell = shellingham.detect_shell()
    except shellingham.ShellDetectionFailure:
        shell = provide_default()
  2. Detect the surrounding shell with detect_shell()

    master

    Use shellingham.detect_shell() to determine which shell the current Python process is running in. It inspects the process's environment to identify the shell type and its executable path.

    Returns a 2-tuple containing:

    1. The shell name (always lowercased).
    2. The command/path used to run the shell.

    If the shell cannot be detected, it raises shellingham.ShellDetectionFailure.

    import shellingham
    shell = shellingham.detect_shell()
    # Returns e.g., ('bash', '/bin/bash')
  3. Handle shell detection failures with ShellDetectionFailure

    master

    When detect_shell() is unable to find a shell process within the specified search depth, it raises shellingham.ShellDetectionFailure. This exception should be caught if you want to provide fallback behavior when the shell environment cannot be determined.

    from shellingham import detect_shell, ShellDetectionFailure
    
    try:
        shell = detect_shell()
    except ShellDetectionFailure:
        # Handle the case where no shell was found
        shell = "unknown"