Streamline bash-command execution from python with a new syntax. It combines the simplicity of writing bash scripts with the flexibility of python. Under the hood, any line or variable assignment starting with $
or surrounded by parentheses is transformed to python subprocess
calls and then injected into sys.meta_path
as an import hook. All possible thanks to the wonderful ideas project!
For security and performance reasons, PyBash will NOT execute as shell, unless explicitly specified with a >
instead of a single $
before the command. While running commands as shell can be convenient, it can also spawn security risks if you're not too careful. If you're curious about the transformations, look at the unit tests for some quick examples.
Note: this is a mainly experimental library.
pip install pybash
from pybash.transformer import transform
transform("$echo hello world") # returns the python code for the bash command as string
pip install "pybash[script]"
text = "HELLO WORLD"
$echo f{text}
python -m pybash hello.py
$python --version
$echo \\nthis is an echo
outputs:
Python 3.9.15
this is an echo
out = $cat test.txt
test_data = out.decode('utf-8').strip()
print(test_data.replace("HELLO", "HOWDY"))
outputs:
HOWDY WORLD
print(($cat test.txt).decode('utf-8').strip())
outputs:
HELLO WORLD
$echo "hello" >> test4.txt
$cat test.txt | sed 's/HELLO/HOWDY/g' | sed 's/HOW/WHY/g' | sed 's/WHY/WHEN/g'
outputs:
WHENDY WORLD
$cat test.txt | sed 's/HELLO/HOWDY\\n/g' > test1.txt >> test2.txt > test3.txt
$cat test.txt | sed 's/HELLO/HOWDY\\n/g' > test5.txt
$sort < test.txt >> sorted_test.txt
$sort < test.txt | sed 's/SORT/TEST\\n/g'
>ls .github/*
Denoted by {{code here}}. Interpolated as direct code replace. The value/output of the variable, function call, or the expression must not include spaces.
## GOOD
command = "status"
def get_option(command):
return "-s" if command == "status" else "-v"
$git {{command}} {{get_option(command)}}
display_type = "labels"
$kubectl get pods --show-{{display_type}}=true
## BAD
option = "-s -v"
$git status {{option}}
options = ['-s', '-v']
$git status {{" ".join(options)}}
# use dynamic interpolation
options = {'version': '-v'}
$git status {{options['version']}}
Denoted by f{ any python variable, function call, or expression here }. Interpolated as f-string. The output of the variable, function call, or the expression must still not include spaces.
## GOOD
# git -h
options = {'version': '-v', 'help': '-h'}
$git f{options['h']}
# kubectl get pods --show-labels -n coffee
namespace = "coffee"
$kubectl get pods f{"--" + "-".join(['show', 'labels'])} -n f{namespace}
## BAD
option = "-s -v"
$git status f{option}
# PYBASH DEMO #
def cp_test():
$cp test.txt test_copy.txt
cp_test()
python -m pybash examples/hello.py
python -m pybash demo
make debug
to view the transformed source code