Skip to main content

Warning

pysh-stub is deprecated and will be deleted. Please, use pysh instead.

Pysh

Bash is said to be the opposite of riding the bike -- you have to re-learn it every time. So if we're doing anything more complex than running a few commands, chances are google will be needed. But if we switch to python, then the reverse is true -- python is a bit too cumbersome for simple scripts. I try to alleviate this problem by making it easier to use bash from python.

It was inspired by Jupyter's way of handling bash.

Installation

pip install pysh-stub

Quickstart

  • If you have a hello_world.pysh file with the contents below, you can run it using pysh hello_world.pysh:
!echo Hello World
print("Hello World")
  • Most bash is going to be one-to-one translatable to pysh by putting exclamation marks at the beginning of each line:
!ls
!cp some_file some/place
  • It has most of the variables ($@, $#, $1, $2, etc) you would expect:
!echo $*
  • You can also access python variables from bash using f-string notation
my_file = "~/some/file" + ".txt"
!cp {my_file} {my_file + ".bak"}
  • If you wish to get the output of any command into a python variable, you can use double exclamation mark:
# Note that the default output of ls is silenced when !! are used
lines = !!ls -lA
for line in lines.splitlines():
    print(line)

Builtins

I consider the most comfortable way to write python scripts is to import os, pathlib, sys, and re first. Thus, all of these modules are pre-imported. However, instead of "pathlib.Path", we have "P".

I also import typer as I consider it to be the most concise way to write any command line application.

If you wish to customize how you call bash subprocesses, you can use the builtin "sh" function. It supports all the same arguments as subprocess.run but has text=True and shell=True by default.

Magical Commands

We use subprocess.run to run bash commands but that has a few quirks. If you modify the process info in any way within the subprocess (cd, set, unset, etc), this information will not change for the currently running process. Hence we have a few magical commands that do not create a subprocess but instead use roughly equivalent python constructs.

Here's a list of all the magical commands and the approximate conversions we apply to them:

Original Converted
!cd arg os.chdir(arg)
!set -e __pysh_check_returncodes__.set(True)
!set +e __pysh_check_returncodes__.set(False)
!exit arg sys.exit(int(arg))

Notes on syntax

  • Each bash call is parsed until the end of the line so they are closer to statements than expressions. Hence the following is not possible:
print(!!ls)
  • Because we use f-strings to interface between python and bash, you will have to escape non-formatting "{" and "}" by typing them twice. You will also have to wrap these characters in quotes because prior to converting bash into executable format, we tokenize the entire file as if it was valid python. I.e. Unclosed braces, parenthesis, etc will cause a tokenize.TokenError.
# The invalid ways to do it
!echo {
!echo {{

# The valid way to do it
!echo "{{"

FAQ

  • If you have any questions, encounter any bugs or want to suggest/contribute new features, please, use the issues in the github repository

Release files for pysh-stub 1.1.4

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

Source distribution (sdist)

Source distribution for pysh-stub 1.1.4
File Size Uploaded
pysh-stub-1.1.4.tar.gz 8.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pysh-stub 1.1.4
File Interpreter ABI Platform
pysh_stub-1.1.4-py3-none-any.whl Python 3 none any Details

Total release size: 16.7 kB

Release files / pysh-stub-1.1.4.tar.gz

Download URL pysh-stub-1.1.4.tar.gz
Size 8.2 kB
Tags Source
SHA-256 checksum
How to use checksums
6a1bdb1ac2c1c8a613330e6631f79f0f03da219ed22fcbe048e95c90d972afd5
BLAKE2b-256 checksum
How to use checksums
96b24efb054e393f5713d7c8fe074f56ea86adc55eefd95502a1f74ecb0172bd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.12 CPython/3.10.4 Linux/5.10.114-1-MANJARO

Release files / pysh_stub-1.1.4-py3-none-any.whl

Download URL pysh_stub-1.1.4-py3-none-any.whl
Size 8.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4a3fb051abb4d053d97b11488475fc3a9dd768371657ca4a4184d5f024544369
BLAKE2b-256 checksum
How to use checksums
790d56d78e441dd1855ac88ceb3634fbb9a823ecb22eb3d4f4185015d0b3baac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.12 CPython/3.10.4 Linux/5.10.114-1-MANJARO

Release history Release notifications | RSS feed

This release

1.1.4 This release

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.3.0

2 release files

0.2.0

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