Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/sharkdp/pysh

Python-enhanced bash scripts
https://github.com/sharkdp/pysh

bash python shell-script

Last synced: about 10 hours ago
JSON representation

Python-enhanced bash scripts

Awesome Lists containing this project

README

        

# pysh
Python-enhanced bash scripts.

[![Build Status](https://travis-ci.org/sharkdp/pysh.svg?branch=master)](https://travis-ci.org/sharkdp/pysh)

`pysh` allows you to write bash scripts that include short snippets of Python
code with a local environment that is shared between bash and Python.

## Example
Lines that start with `#> ` are evaluated as Python:
```bash
##### From bash to python #####

bashVariable="Hello world"
#> print("{} from python!".format(bashVariable))

##### From python to bash #####

#> pythonVariable = " ".join(["Hello", "world"])
echo "$pythonVariable from bash!"

######## Back and forth #######

for file in *.csv; do
echo
echo "before: $file"

#> base, ext = os.path.splitext(file)
#> file = base.upper() + ext

echo "after: $file"
done
```

Run it:
```
> ls
dummy.csv important.csv example.sh

> pysh example.sh
Hello world from python!
Hello world from bash!

before: dummy.csv
after: DUMMY.csv

before: important.csv
after: IMPORTANT.csv
```

## Caveats

This is only supported for Python 3.4 and above due to [`redirect_stdout`](https://docs.python.org/3/library/contextlib.html#contextlib.redirect_stdout).

## Disclaimer / warning
This is a prototype implementation with lots of evil hacks.