Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
85 lines (65 sloc) 2.69 KB

Plumbum: Shell Combinators

Ever wished the wrist-handiness of shell scripts be put into a real programming language? Say hello to Plumbum Shell Combinators. Plumbum (Latin for lead, which was used to create pipes back in the day) is a small yet feature-rich library for shell script-like programs in Python. The motto of the library is "Never write shell scripts again", and thus it attempts to mimic the shell syntax ("shell combinators") where it makes sense, while keeping it all pythonic and cross-platform.

Apart from shell-like syntax and handy shortcuts, the library provides local and remote command execution (over SSH), local and remote file-system paths, easy working-directory and environment manipulation, and a programmatic Command-Line Interface (CLI) application toolkit. Now let's see some code!

This is only a teaser; the full documentation can be found at Read the Docs

Cheat Sheet


>>> from plumbum import local, FG, BG
>>> from plumbum.cmd import ls, grep, wc, cat, head
>>> ls
LocalCommand(<LocalPath /bin/ls>)
>>> ls()


>>> chain = ls["-a"] | grep["-v", "\\.py"] | wc["-l"]
>>> print chain
/bin/ls -a | /bin/grep -v '\.py' | /usr/bin/wc -l
>>> chain()


>>> ((cat < "") | head["-n", 4])()
u'#!/usr/bin/env python\nimport os\n\ntry:\n'
>>> (ls["-a"] > "file.list")()
>>> (cat["file.list"] | wc["-l"])()

Working-directory manipulation

>>> local.cwd
<Workdir /home/tomer/workspace/plumbum>
>>> with local.cwd(local.cwd / "docs"):
...     chain()

Foreground and background execution

>>> (ls["-a"] | grep["\\.py"]) & FG         # The output is printed to stdout directly
>>> (ls["-a"] | grep["\\.py"]) & BG         # The process runs "in the background"
<Future ['/bin/grep', '\\.py'] (running)>

Command nesting

>>> from plumbum.cmd import sudo
>>> print sudo[ifconfig["-a"]]
/usr/bin/sudo /sbin/ifconfig -a
>>> (sudo[ifconfig["-a"]] | grep["-i", "loop"]) & FG
lo        Link encap:Local Loopback
          UP LOOPBACK RUNNING  MTU:16436  Metric:1

Remote commands (over SSH)

>>> from plumbum import SshMachine
>>> remote = SshMachine("")
>>> r_ls = remote["ls"]
>>> with remote.cwd("/lib"):
...     (r_ls | grep[""])()
Something went wrong with that request. Please try again.