Run safe and cross-platform bash commands using Python 3.14's t-strings
Project description
[!WARNING] WIP. Until v1.0.0 is released, API is subject to drastic changes.
Run safe and cross-platform bash commands using Python 3.14's t-strings
Uses brush for a cross-platform bash implementation.
from tshu import sh
await sh(t"uv add tshu")
Installation
uv add tshu
Usage
from tshu import sh
Run a bash command.
Always use a t-string, regular strings are not allowed to prevent accidental usage of f-strings.
await sh(t"cat /usr/share/dict/words | wc -l")
Run a bash command with user input
You still need to quote substitutions to prevent word-splitting. Word-splitting is a feature, not a vuln.
Substitutions use variables to prevent shell injection. These temporary variables are not accessible by child programs as they are not exported and have random names.
await sh(t'cat "{__file__}" | wc -l')
Exit code and check
By default, awaiting a command returns the exit code. [tshu.CommandError][] is raised
when a command returns a non-zero exit code. To disable this behaviour, either
pass check=False to sh or set sh.check = False to disable checking globally.
Suppress command output
Either pass quiet=True or set sh.quiet = True to suppress output globally.
Change working directory
Either pass cwd=... or set sh.cwd = "...". You can use pathlib.Path.
Set environment variables
Either modify os.environ or pass env={}. These are exported, so accessible to
child programs, use substitutions to pass user input.
Pass standard input
input can be string or bytes.
assert await sh(t"wc -l", input="1\n2\n3\n").json() == 3
Capture the stdout, stderr of a program
result = await sh(t"help").output()
result.returncode
result.stdout
result.stderr
Get standard output directly
Use .text() to directly get the output as string (utf-8 encoded).
contents = await sh(t"cat file.txt").text()
Get standard output directly as bytes
contents = await sh(t"cat file.bin").bytes()
Get standard output parsed as JSON
data = await sh(t"cat file.json").json()
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 tshu-0.1.2.tar.gz.
File metadata
- Download URL: tshu-0.1.2.tar.gz
- Upload date:
- Size: 36.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7af4216b5d3fe3a88c39646c03824d3550fd1406d6e56d179519f75652a75923
|
|
| MD5 |
0430a62f0f13e54c8a8495b35f7017cb
|
|
| BLAKE2b-256 |
c9db3021d7f11cb672591b1f9c2e62382a3e0a0255982663022484de73599cc6
|
File details
Details for the file tshu-0.1.2-cp314-cp314-win_amd64.whl.
File metadata
- Download URL: tshu-0.1.2-cp314-cp314-win_amd64.whl
- Upload date:
- Size: 2.7 MB
- Tags: CPython 3.14, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
badee34ef68d2a95404114f6d377daa01fd31b75d25e2027e43481dbed11b8c6
|
|
| MD5 |
4b9d817e98a59788fcbbbc8c69ea28ba
|
|
| BLAKE2b-256 |
71dae75383aa515174acf7c85ea63a301c41d71a11f8186a2962eda264d0eea6
|