| 1 |
============================================= |
| 2 |
Shellingham: Tool to Detect Surrounding Shell |
| 3 |
============================================= |
| 4 |
|
| 5 |
.. image:: https://img.shields.io/pypi/v/shellingham.svg |
| 6 |
:target: https://pypi.org/project/shellingham/ |
| 7 |
|
| 8 |
Shellingham detects what shell the current Python executable is running in. |
| 9 |
|
| 10 |
|
| 11 |
Usage |
| 12 |
===== |
| 13 |
|
| 14 |
.. code-block:: python |
| 15 |
|
| 16 |
>>> import shellingham |
| 17 |
>>> shellingham.detect_shell() |
| 18 |
('bash', '/bin/bash') |
| 19 |
|
| 20 |
``detect_shell`` pokes around the process's running environment to determine |
| 21 |
what shell it is run in. It returns a 2-tuple: |
| 22 |
|
| 23 |
* The shell name, always lowercased. |
| 24 |
* The command used to run the shell. |
| 25 |
|
| 26 |
``ShellDetectionFailure`` is raised if ``detect_shell`` fails to detect the |
| 27 |
surrounding shell. |
| 28 |
|
| 29 |
|
| 30 |
Notes |
| 31 |
===== |
| 32 |
|
| 33 |
* The shell name is always lowercased. |
| 34 |
* On Windows, the shell name is the name of the executable, minus the file |
| 35 |
extension. |
| 36 |
|
| 37 |
|
| 38 |
Notes for Application Developers |
| 39 |
================================ |
| 40 |
|
| 41 |
Remember, your application's user is not necessarily using a shell. |
| 42 |
Shellingham raises ``ShellDetectionFailure`` if there is no shell to detect, |
| 43 |
but *your application should almost never do this to your user*. |
| 44 |
|
| 45 |
A practical approach to this is to wrap ``detect_shell`` in a try block, and |
| 46 |
provide a sane default on failure |
| 47 |
|
| 48 |
.. code-block:: python |
| 49 |
|
| 50 |
try: |
| 51 |
shell = shellingham.detect_shell() |
| 52 |
except shellingham.ShellDetectionFailure: |
| 53 |
shell = provide_default() |
| 54 |
|
| 55 |
|
| 56 |
There are a few choices for you to choose from. |
| 57 |
|
| 58 |
* The POSIX standard mandates the environment variable ``SHELL`` to refer to |
| 59 |
"the user's preferred command language interpreter". This is always available |
| 60 |
(even if the user is not in an interactive session), and likely the correct |
| 61 |
choice to launch an interactive sub-shell with. |
| 62 |
* A command ``sh`` is almost guaranteed to exist, likely at ``/bin/sh``, since |
| 63 |
several POSIX tools rely on it. This should be suitable if you want to run a |
| 64 |
(possibly non-interactive) script. |
| 65 |
* All versions of DOS and Windows have an environment variable ``COMSPEC``. |
| 66 |
This can always be used to launch a usable command prompt (e.g. `cmd.exe` on |
| 67 |
Windows). |
| 68 |
|
| 69 |
Here's a simple implementation to provide a default shell |
| 70 |
|
| 71 |
.. code-block:: python |
| 72 |
|
| 73 |
import os |
| 74 |
|
| 75 |
def provide_default(): |
| 76 |
if os.name == 'posix': |
| 77 |
return os.environ['SHELL'] |
| 78 |
elif os.name == 'nt': |
| 79 |
return os.environ['COMSPEC'] |
| 80 |
raise NotImplementedError(f'OS {os.name!r} support not available') |