xonsh: Python-powered Shell
xonsh (pronounced 'consh') is a modern, full-featured, and cross-platform shell language and command prompt powered by Python. It seamlessly integrates Python 3 with shell functionality, allowing users to mix Python code and shell commands. The current version is 0.22.8, and it maintains a regular release cadence, supporting the latest minor Python versions (currently Python >=3.11).
Common errors
-
ModuleNotFoundError: No module named 'some_package'
cause The Python package was installed in a different virtual environment than the one xonsh is currently running in.fixEnsure xonsh is running in the correct environment where the package is installed. Use `xpip install some_package` to install packages directly into the xonsh environment, or launch xonsh from within the activated virtual environment that contains the desired packages. Use `xcontext` to inspect the current xonsh environment. -
bash: module: line 1: syntax error: unexpected end of file
cause Conflicts with Bash configurations, particularly on Linux systems, when xonsh tries to load certain shell modules or functions defined in `.bashrc`.fixAdd `unset module` and `unset scl` to your `~/.bashrc` file to prevent Bash from attempting to import these functions when xonsh launches. -
xonsh --version returns an old version, or 'xonsh' already seems to be installed, Not modifying existing installation in ~/.local/share/pipx/venvs/xonsh'. Pass '–force' to force installation.
cause An older version of xonsh is detected by a package manager or an IDE extension, preventing the installation or update of a newer version.fixManually uninstall the old xonsh version using `pipx uninstall xonsh` or `pip uninstall xonsh` within your WSL or relevant environment. Then, proceed with the desired installation method (e.g., `pip install 'xonsh[full]'`) or allow your IDE/tool to reinstall it.
Warnings
- breaking Aliases `ipynb` and `scp-resume` were removed from built-in aliases, and `xonsh-reset` was renamed to `xreset`.
- gotcha Installing xonsh via system package managers (e.g., `apt`, `pacman`, `brew`) is not recommended, especially as a login shell. This can lead to outdated versions, missing dependencies, and potential breakage with system Python updates.
- gotcha Xonsh is a non-POSIX compatible shell. Attempting to use it as a login shell via `chsh` or expecting it to execute Bash scripts directly or perfectly translate all Bash idioms can lead to unexpected behavior or breakage.
- gotcha Quotes in the middle of a shell command argument are handled as literal parts of the argument in xonsh, unlike traditional shells where they might be removed. This can lead to unexpected parsing by external commands.
Install
-
pip install 'xonsh[full]' --user -
conda install xonsh -c conda-forge -
TARGET_DIR=$HOME/.local/xonsh-env PYTHON_VER=3.11 XONSH_VER='xonsh[full]' \ /bin/bash -c "$(curl -fsSL https://xon.sh/install/mamba-install-xonsh.sh)"
Imports
- @
from xonsh import @
This is a special global object automatically available in the xonsh shell session. It doesn't require an explicit import statement.
- xonsh.lib.lazyjson
from xonsh import lazyjson
from xonsh.lib import lazyjson
- xonsh.lib.lazyasd
from xonsh import lazyasd
from xonsh.lib import lazyasd
Quickstart
import os
# To run xonsh, simply execute 'xonsh' in your terminal after installation.
# This example demonstrates mixing Python and shell commands within a .xonshrc file or interactively.
# Python mode: Assign a variable
name = 'xonsh_user'
# Subprocess mode: Echo the variable using Python substitution
print(f"echo Hello, @(name)!")
# Mixing Python and shell for a loop
for i in range(3):
print(f"echo Loop iteration: @(i)")
# Accessing environment variables (Python style)
path_var = os.environ.get('PATH', '')
print(f"Current PATH has {len(path_var.split(os.pathsep))} entries.")
# Running a shell command and capturing its output (subprocess style)
output = $(ls -l)
print(f"'ls -l' command output length: {len(output)}")