A Python library for managing persistent shell sessions programmatically
Project description
ShellSh
A Python library for managing persistent shell sessions programmatically with non-blocking I/O.
Features
- Non-blocking command execution - Commands return immediately without waiting for completion
- Persistent shell sessions - Maintain environment variables, working directory, and state between commands
- Real-time output streaming - Capture output from long-running processes incrementally
- Interactive program support - Works with curses-based programs like
top,vim, etc. - Process control - Stop running commands with Ctrl+C programmatically
Installation
pip install shellsh
Quick Start
from shellsh import ShellSh
import time
# Create a shell session
sh = ShellSh("my_session")
# Non-blocking mode (default)
sh.typeenter("ls -la")
time.sleep(0.5)
print(sh.flush()) # Get the output
# Blocking mode - waits for command completion
sh.setblocking(True)
sh.typeenter("sleep 2; echo 'Done'") # This will block for ~2 seconds
print(sh.flush()) # Output is ready immediately after typeenter returns
# Back to non-blocking
sh.setblocking(False)
sh.typeenter("echo 'Non-blocking'")
time.sleep(0.1)
print(sh.flush())
# Environment persists between commands
sh.typeenter("cd /tmp")
sh.typeenter("pwd")
time.sleep(0.5)
print(sh.flush()) # Prints: /tmp
# Stop long-running commands
sh.typeenter("sleep 100")
time.sleep(1)
sh.stop() # Sends Ctrl+C
# Clean up
sh.close()
API Reference
ShellSh(name)
Create a new shell session with the given name.
typeenter(line)
Send a command to the shell. By default, returns immediately without waiting for execution. If setblocking(True) was called, blocks until the command completes.
flush()
Retrieve new output since the last flush. Returns only unread output.
setblocking(blocking)
Set blocking mode for typeenter().
blocking=True:typeenter()waits for command completion before returningblocking=False:typeenter()returns immediately (default)
stop()
Send Ctrl+C to stop the currently running command.
close()
Terminate the shell session and clean up resources.
Context Manager
ShellSh supports context manager protocol:
with ShellSh("session") as sh:
sh.typeenter("echo Hello")
time.sleep(0.5)
print(sh.flush())
# Automatically closed
Use Cases
- Automating shell interactions
- Building terminal-based tools
- Testing command-line applications
- Managing long-running processes
- Creating interactive shell wrappers
Requirements
- Python 3.7+
- Unix-like operating system (Linux, macOS)
License
MIT
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file shellsh-0.2.0.tar.gz.
File metadata
- Download URL: shellsh-0.2.0.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5289dac3a87994049c23dcdf0e746027c50ee1c44ba65e4c45fe52944ab8d6ef
|
|
| MD5 |
f1a94d6098bf2a1c73176743ba7b3378
|
|
| BLAKE2b-256 |
a647a9d5253e90c2882e2659cb6523214c99c8506d8ae9c4b10a41bce1cd87ea
|
Provenance
The following attestation bundles were made for shellsh-0.2.0.tar.gz:
Publisher:
publish.yml on saturnino-adrales/shellsh
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
shellsh-0.2.0.tar.gz -
Subject digest:
5289dac3a87994049c23dcdf0e746027c50ee1c44ba65e4c45fe52944ab8d6ef - Sigstore transparency entry: 513279900
- Sigstore integration time:
-
Permalink:
saturnino-adrales/shellsh@4ca74dc95201805f3a9578dcb10a744c4edad7af -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/saturnino-adrales
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@4ca74dc95201805f3a9578dcb10a744c4edad7af -
Trigger Event:
release
-
Statement type:
File details
Details for the file shellsh-0.2.0-py3-none-any.whl.
File metadata
- Download URL: shellsh-0.2.0-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
33b92ee2cfa094042b396d356dedc6a3a757c08fa9c7363f585fd8cc13bff015
|
|
| MD5 |
33706e4cc1d27f2a27ebdd0bebb74d61
|
|
| BLAKE2b-256 |
5caed1011c434b0bf738ec86afbff1d6645ba1f2cdffe84709b879e936043c1d
|
Provenance
The following attestation bundles were made for shellsh-0.2.0-py3-none-any.whl:
Publisher:
publish.yml on saturnino-adrales/shellsh
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
shellsh-0.2.0-py3-none-any.whl -
Subject digest:
33b92ee2cfa094042b396d356dedc6a3a757c08fa9c7363f585fd8cc13bff015 - Sigstore transparency entry: 513279903
- Sigstore integration time:
-
Permalink:
saturnino-adrales/shellsh@4ca74dc95201805f3a9578dcb10a744c4edad7af -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/saturnino-adrales
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@4ca74dc95201805f3a9578dcb10a744c4edad7af -
Trigger Event:
release
-
Statement type: