Xonsh Shell：在 Linux 终端结合 Bash Shell 和 Python 的最佳特性
Python Xonsh shell
![](/data/attachment/album/202203/24/111824ufw1815lyvclnsr2.jpg)


最受欢迎的 shell 是什么？我猜你会回答 bash 或者 zsh，没错，的确如此。


UNIX 和 Linux 系统上有许多可用的 shell，包括 Bash、Ksh、Zsh、Fish 等等。


最近，我碰到了另外一个 shell，它结合了 Python 和 bash，还蛮特别的。


### Xonsh shell，为喜爱 Python 的 Linux 用户而打造


[Xonsh](https://xon.sh/) 是一个使用 Python 编写的跨平台 shell 语言和命令提示符。


它结合了 Python 和 Bash shell，因此你可以在这个 shell 中直接运行 Python 命令（语句）。你甚至可以把 Python 命令和 shell 命令混合起来使用。


![](/data/attachment/album/202203/24/111830ba1hfqb535pv5qyu.png)


听起来不错吧？如果你是一个 Python 程序员的话，你应该会更加兴奋。


### 在 Linux 上安装 Xonsh shell


有多种方式可以在 Linux 系统上安装 Xonsh shell。


你可以使用软件包管理器安装它（主要的 Linux 发行版的仓库中都有 Xonsh shell）。


在 Ubuntu 和 Debian 上，你可以尝试运行：



```
sudo apt install xonsh

```

因为它本质上是一个 Python 应用，所以你总是可以使用 Pip 来安装（如果你的系统已经安装了的话）。或者，你也可以使用 Conda 来安装 Xonsh。同时，它还提供了 AppImage 格式和 Docker 镜像。


你可以自由选择你喜欢的安装方式。


### Xonsh shell 初体验


如果你不熟悉 Linux 的“多 shell”概念和终端的相关知识，我推荐你阅读 [我写的一篇终端入门的文章](https://itsfoss.com/basic-terminal-tips-ubuntu/)。这篇文章会帮助 Linux 新手弄清楚很多东西。


我在这里简单介绍一下。你的终端运行着一个默认的 shell（通常是 bash shell）。如果你在系统上安装了其他的 shell，你可以 [轻松地切换这些 shell](https://linuxhandbook.com/change-shell-linux/)。


要进入 Xonsh shell，只需运行一条命令：



```
xonsh

```

![](/data/attachment/album/202203/24/111831iuzvdr65bvu5mp32.png)


它会建议你运行一个设置向导，然后创建一个 Xonsh shell 的自定义 `rc` 配置文件。这是可选的，你也可以晚些时候再做。


一旦你开始使用它，你就立刻会感觉到一些不同。


当你开始输入一条命令时，Xonsh 会自动建议历史记录中的最后一条匹配命令。你可以按下右箭头键确认使用这条自动建议的命令。你也可以按下上、下箭头键来选择历史记录中的其他匹配命令。


![Xonsh shell automatically suggests last matching command from the history as you type](/data/attachment/album/202203/24/111831h6ersxxszi1ertrr.png)


不仅如此，它还支持交互式的 Tab 补全。当你开始输入命令，并按下 `tab` 键，它会显示一个类似于下拉式的交互式菜单。你可以在这个菜单中选择可用的命令。


![Tab completion in Xonsh shell](/data/attachment/album/202203/24/111831l5uxb8xwv78oz9zz.png)


你可以混合使用 Python 和 bash 命令，并且直接在 shell 中运行它们。


如果你对 [终端定制](https://itsfoss.com/customize-linux-terminal/) 感兴趣，你可以花点时间和精力，根据自己的喜好来定制 Tab 补全、按键绑定和颜色样式。


它同时拥有一个叫做 [Xontrib](https://github.com/topics/xontrib) 的用户自定义插件系统，你可以使用这些插件来扩展 Xonsh shell 的功能。


如果你是一个终端爱好者，并渴望学习更多终端知识，你可以浏览它的 [丰富的文档系统](https://xon.sh/tutorial.html)。


### 最后


大多数用户坚持使用默认的 Bash shell，这很正常。有一些开发者和系统管理员会选择使用 Zsh，因为它有很多附加特性。而 Xonsh 完全聚焦在 Python 上，对于 Python 使用者来说，它或许具有吸引力。对于其余的人来说，Bash 已经足够好了。


你使用过 Xonsh 或者其他的 shell 吗？你用它来代替 Bash 的原因是什么呢？欢迎在评论区留言。




---


via: <https://itsfoss.com/xonsh-shell/>


作者：[Abhishek Prakash](https://itsfoss.com/author/abhishek/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[lkxed](https://github.com/lkxed) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
![Warp Terminal](/assets/images/warp-terminal.webp)


![Warp Terminal](/assets/images/warp-terminal.webp)

Which is the most popular shell? I guess you’ll say bash or perhaps zsh and you are right about that.

There are several shells available for UNIX and Linux systems. bash, ksh, [zsh](https://itsfoss.com/zsh-ubuntu/), fish and more.

Recently, I came across another shell which offers a unique twist of combining Python and bash.

## Xonsh shell for the Python loving Linux users

[Xonsh](https://xon.sh/?ref=itsfoss.com) is a Python-powered, cross-platform, shell language and command prompt.

It combines Python and bash shell in a way that you can run Python commands directly in the shell. You can even combine Python and shell commands.

![xonsh shell combines python bash](https://itsfoss.com/content/images/wordpress/2022/03/xonsh-shell-combines-python-bash-800x470.png)

Sounds good? It may sound even better if you are a Python programmer.

## Installing Xonsh shell on Linux

There are several ways you can install Xonsh shell on your system.

You can install it from your distribution’s package manager (it is available in the repositories of major distributions).

On Ubuntu and Debian, you may try:

`sudo apt install xonsh`


Since it is essentially a Python application, you always have the option to use Pip (if you have pip installed on your system). [Conda](https://learnubuntu.com/install-conda/?ref=itsfoss.com) can also be used for installing Xonsh. There is also an AppImage available along with a Docker image.

You may choose whichever installation method you prefer.

## Experiencing the Xonsh shell

If you are new to the multiple shell concept or the terminal, I suggest reading [my article on terminal introduction](https://itsfoss.com/basic-terminal-tips-ubuntu/). It will help clear numerous things for the Linux beginners.

Quick recap. Your terminal runs a default shell (usually bash shell). If you have other shells installed on your system, you can [easily switch the shells](https://linuxhandbook.com/change-shell-linux/?ref=itsfoss.com).

To enter the Xonsh shell, simply run this command:

`xonsh`


![xonsh shell](https://itsfoss.com/content/images/wordpress/2022/03/xonsh-shell-800x451.png)

It advises you to run a wizard and create a customized rc file for Xonsh shell. It’s optional and you may do it later as well.

You can start feeling a bit of difference as soon as you start using it.

As you start typing a command, Xonsh shell automatically suggests the last matching command from the history. You can press the right arrow key to confirm it and use the auto suggested command. You can press the up and down arrow key to go back to other matching commands from the history.

![auto suggestions from history in xonsh shell](https://itsfoss.com/content/images/wordpress/2022/03/auto-suggestions-from-history-in-xonsh-shell-800x258.png)

That’s not it. The tab completion is interactive here. Start typing the command and press tab and it shows a dropdown kind of interactive menu. From here, you can choose between the available commands.

![tab completion xonsh shell](https://itsfoss.com/content/images/wordpress/2022/03/tab-completion-xonsh-shell-800x354.png)

You can combine the Python and bash commands and run them directly in the shell.

If you are into [terminal customization](https://itsfoss.com/customize-linux-terminal/), you can spend some time and effort to customize tab completion, key bindings and color styles as per your preference.

It also has a user generated plugin systems called [Xontrib](https://github.com/topics/xontrib?ref=itsfoss.com) and you can use them to extend the functionality of the Xonsh shell.

If you are a terminal lover and interested in learning more on it, you can go through its [extensive documentation system](https://xon.sh/tutorial.html?ref=itsfoss.com).

## In the end…

Most users stick with the default bash shell and there is nothing wrong with it. Some developers and sysadmins opt for Zsh for its additional features. Xonsh focuses entirely on Python and it may attract Python users. For the rest of us, bash is good enough.

Have you ever used Xonsh or some other shell? Why do you use it instead of bash? The comment section is all yours.