CategoriesPython

Introduction to Xonsh

Recently, I got started with xonsh (pronounced “conch”) as a replacement shell for Bash.

What is xonsh, you might ask? Well, basically, it’s a version of Python meant for use as a shell. Since it’s a superset of Python, all Python programs are valid xonsh shell scripts, so you can make use of Python’s standard library and any other Python package you have available.

Probably my favorite feature, though, is being able to transfer my Python knowledge to shell scripting. As the feature comparison puts it, xonsh is a “sane language.”

That means we can do math (and a lot more!) directly in the shell, like so:

$ (5 + 5) ** 5
100000

However, we can also write commands, just like in Bash:

$ curl example.com
<!doctype html>
<html>
<head>
    <title>Example Domain</title>
...

Xonsh handles this by having two modes, which it automatically chooses between for each line. Python mode is the default, but any time a line contains only an expression statement, and the names are not all current variables, it will be interpreted as a command in subprocess mode.

When you install xonsh and run it without a .xonshrc file, you’ll be presented with a welcome screen:

            Welcome to the xonsh shell (0.9.13.dev1)                              

            ~ The only shell that is also a shell ~                              

----------------------------------------------------
xonfig tutorial   ->    Launch the tutorial in the browser
xonfig wizard     ->    Run the configuration wizard and claim your shell 
(Note: Run the Wizard or create a ~/.xonshrc file to suppress the welcome screen)

Going through the wizard will present you with a ton of options. Most will not be necessary to mess with, but there are some useful things like changing the prompt, and various plugins.

That’s just the tip of the iceberg. Xonsh has a lot more to offer, but I’m still exploring the possibilities.

For further reading, the guides cover a lot of topics in-depth.