xonsh: A revolutionary Python-Bash hybrid shell

I just found out about xonsh. I will just paste a bit from the project’s homepage:

Xonsh is a Python-ish, BASHwards-looking shell language and command prompt. The language is a superset of Python 3.4+ with additional shell primitives that you are used to from Bash and IPython. It works on all major systems including Linux, Mac OSX, and Windows. Xonsh is meant for the daily use of experts and novices alike.

To install it on Ubuntu Yakkety 16.10 just do sudo apt-get install xonsh then go into your favorite terminal program (I like Konsole) and create a new profile. Name it xonsh and for the command option set it to /usr/bin/xonsh.

Then you just open a new tab or window with the xonsh profile and the first time it runs it will prompt to run through a setup script. Most of the defaults are good so you can just press enter. I did choose the option to enable compatibility with a foreign shell and selected Bash.

Based on the answers it will create the static configuration file at ~/.config/xonsh/config.json.

You can also create a user control file ~/.xonshrc. See here for more information on how to set this up. You can add Python modules and statements inside .xonshrc. For example I added import os so that I can now use commands like os.getcwd() in the shell.

If you want just a particular method in a library you can use for example: from math import pi and then in the shell if you enter pi you will see the number pi. Another library that might be really useful to import would be the regex library: import re

Once you’re finished editing .xonshrc you source it just like you would a Bash file: source .xonshrc If you want to source a Bash file there’s a built-in alias for it: source-bash

After setting up xonsh you have an amazing shell environment which essentially behaves like Bash yet has the full power and features of a Python shell as well.

I’ve already switched to it as my default profile in Konsole. I’ve been trying some shell stuff like echo grep cut find etc. and it all works. I also have been doing some stuff with Python lists, iterators, and loops and it all works well.

I’m not an ultimate shell-guru person or anything but to me xonsh seems revolutionary.

Here’s a portion of a comment on a forum by the creator of xonsh regarding Bash compatibility:

… I agree that making shell languages into ‘real’ languages is strong goal that we should all have. And like with real languages, many can co-exist happily.

I’d like to point out though, that the goal is not to have bash-compatibility. As you point out, bash does some insane things and that is exactly what I want to avoid with xonsh. I should be able to give xonsh to a programming newbie and they should not experience any gotchas once they know the language.

I have been pretty careful to say BASHwards-compatibility in the docs and elsewhere, for exactly the reasons you mention. The goal isn’t to be bash with some python-isms, but to be python with the useful parts of the shell syntax. Like any good technology, it should interface as well as it can (ie when not insane to do so) with the previous technologies. …

References:

infoworld.com: New shell packs power of Python and Bash


Comments

Leave a Reply