Skip to main content

shell-next logo

shell-next

CI Coverage: 100% required PyPI Python License: MIT

Persistent asynchronous Bash, PowerShell, and cmd sessions for Python 3.14+.

Documentation · PyPI · Wiki drafts

from shell_next import Backend, ProcessCommand, SessionConfig, use_shell_session


async def inspect_repository(config: SessionConfig):
    async with use_shell_session(config) as shell:
        await shell.chdir("/srv/project")
        result = await shell.run(ProcessCommand("git", ("status", "--short")), check=True)
        return result.stdout.tail

Use ProcessCommand for executable arguments that must remain structural. Use SessionScript for backend-native scripts and persistent shell variables, functions, and aliases. Shell syntax is never translated between languages.

Install with python -m pip install shell-next. Supply SessionConfig(backend=Backend.BASH) on Linux, or select Backend.POWERSHELL (PowerShell 7 installed) or Backend.CMD on Windows. Python 3.14+ is required.

Interactive commands

from shell_next import CommandOptions, StdinMode


async def answer_prompt(shell, program):
    options = CommandOptions(stdin=StdinMode.MANUAL)
    async with shell.command(program, options=options, timeout=30) as command:
        await command.expect(b"Name: ")
        await command.sendline(b"Ada")
        await command.close_stdin()
        return await command.wait()

submit() immediately registers a command and returns its handle. One session executes one command at a time; the default policy rejects concurrent submissions. ConcurrencyPolicy.QUEUE enables FIFO queuing. Different sessions run independently.

Cancelling wait() cancels observation only. Cancelling run() or escaping a command context requests termination. A force-stopped shell becomes unusable; create another session explicitly when required.

Deterministic application tests

from shell_next import Emit, MockExpectation, MockScenario, MockShellSession

scenario = MockScenario(
    [
        MockExpectation(ProcessCommand("git", ("status", "--short")), (Emit(b" M README.md\n"),)),
    ]
)
config = SessionConfig()
config._session_cls = MockShellSession.configured(scenario)

Pass this same configuration to application code using use_shell_session. The mock shares production lifecycle code while keeping execution, state, input, and output in memory. Unmatched commands and unconsumed strict expectations fail. Advance(60) advances mock time without sleeping. Plain config._session_cls = MockShellSession creates an empty strict scenario.

Capture and capabilities

Output is bytes. Each stream retains a bounded tail (64 KiB by default), and literal prompt matching uses a separate bounded window. Slow event subscribers receive an overflow error without blocking primary capture. Opt into full capture with CaptureConfig(directory=existing_directory). Results distinguish received bytes, durable file bytes, truncation, sealing, and possible unread output.

Inspect shell.capabilities before requesting backend-specific behavior. Windows active elevation and terminal/PowerShell Host prompt automation are unsupported. The library is a process-management API, not a security sandbox for untrusted scripts.

See the development specification, usage guide, architecture, and release checklist.

Development

python -m pip install -e ".[dev]"
python -m ruff check .
python -m mypy
python -m pytest --cov --cov-branch
python -m build
python -m twine check dist/*

The package uses only the Python standard library at runtime. Tests that start real shells are marked integration. Test files mirror production responsibilities; reusable test infrastructure lives in tests/support.

Metadata

Release files for shell-next 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for shell-next 0.1.1
File Size Uploaded
shell_next-0.1.1.tar.gz 1.8 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for shell-next 0.1.1
File Interpreter ABI Platform
shell_next-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 1.9 MB

Release files / shell_next-0.1.1.tar.gz

Download URL shell_next-0.1.1.tar.gz
Size 1.8 MB
Tags Source
SHA-256 checksum
How to use checksums
bfe5adaad290f6267c8b2227d82a41a1deffa6bdaa52ed47f2a684ea205b9058
BLAKE2b-256 checksum
How to use checksums
73a563c324e4a75377f02e25f42c03d4a677a384b00d78fdfeee78d750664054
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.2

Release files / shell_next-0.1.1-py3-none-any.whl

Download URL shell_next-0.1.1-py3-none-any.whl
Size 64.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c6ed8667858f2a21065e0a5014a0989be5afff1c08c9226fc3fc8e58e2e00954
BLAKE2b-256 checksum
How to use checksums
fbd777551e70db0a61b8365579b4aef6b01a78eb8520831753d876984ff65c75
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.2

Release history Release notifications | RSS feed

0.1.3

2 release files

0.1.2

2 release files

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page