Skip to main content

Run safe and cross-platform bash commands using Python 3.14's t-strings

Project description

★ Star the repo to support the project!

[!WARNING] WIP. Until v1.0.0 is released, API is subject to drastic changes.

image image image Discord

Documentation

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 Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

tshu-0.1.3-cp314-cp314-win_amd64.whl (2.7 MB view details)

Uploaded CPython 3.14Windows x86-64

tshu-0.1.3-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.0 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

tshu-0.1.3-cp314-cp314-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (2.7 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ ARMv7l

tshu-0.1.3-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.7 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ ARM64

File details

Details for the file tshu-0.1.3-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: tshu-0.1.3-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.22

File hashes

Hashes for tshu-0.1.3-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 83165e1bd2f3d0292d6a324a56503dece1b7235503fd3074b48e889882060870
MD5 65b9a6961f5cb5a8876489e6fd60bd47
BLAKE2b-256 2f57cdc2a80d3849078af8f34741e0b18e383985c2fb5443ad73bedc3216958b

See more details on using hashes here.

File details

Details for the file tshu-0.1.3-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for tshu-0.1.3-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 903c3772686e6a1161164268e2891f20ef40b82af7ce98aaca28ed0906fa86a7
MD5 b26b69b5d44ced3524fc8438ce485c62
BLAKE2b-256 c972a3fb1d0b0c90aa8f375fc8871fd785ab7c38184193a80d585d7a4d35a897

See more details on using hashes here.

File details

Details for the file tshu-0.1.3-cp314-cp314-manylinux_2_17_armv7l.manylinux2014_armv7l.whl.

File metadata

File hashes

Hashes for tshu-0.1.3-cp314-cp314-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 76134616099c0cb2562b0ed118d2e5bc13cf0d3b606bd464ba65dc9ac81f4d0c
MD5 3bd8bab3e6bd0e83fe68b1b3e357b22a
BLAKE2b-256 f4f391cda97c20dd6a8b6ee4100eab2df18d8a0d28a954ee256355ad88307c86

See more details on using hashes here.

File details

Details for the file tshu-0.1.3-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for tshu-0.1.3-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 0490ca1af295e2a33720760118658caf24392f92fa4e9ca5b80ed021683d57e1
MD5 345254d74d3e4cd953ea70b5bbcdc878
BLAKE2b-256 91c0660f3a0bb41cb1f433ed09f3ee0c7daa968efa6fdbe26abb21f713529df9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page