Learn XONSH with Real Code Examples
Updated Nov 26, 2025
Code Sample Descriptions
1
Hello World in Xonsh
print("Hello World")
A simple Hello World example in Xonsh using Python syntax.
2
Mixing Shell and Python in Xonsh
echo @(2 + 3)
ls | grep py
An example combining shell commands and Python in Xonsh.
3
Xonsh Variable and Shell Command
name = "Saurav"
echo Hello @(name)
Demonstrates creating a Python variable and using it inside a shell command.
4
Xonsh Inline Python Math
echo Result is @(10 * 20)
Using Python expressions inline inside shell commands.
5
List Files with Python Loop
for f in $(ls):
print(f)
Loops in Python while running shell commands inside Xonsh.
6
Environment Variables in Xonsh
$ENV_VAR = "xonshrocks"
echo $ENV_VAR
Setting and accessing environment variables using Python dict-style syntax.
7
Xonsh Aliases Example
aliases['hi'] = lambda: print('Hello from alias!')
hi
Defines an alias in Xonsh using Python syntax.
8
Python List Comprehension in Xonsh
nums = [n*n for n in range(5)]
print(nums)
Using list comprehensions inside a Xonsh script.
9
Using Subprocess in Xonsh
out = $(echo hello)
print(out)
Runs a shell command and stores its output into a Python variable.
10
Xonsh Python Function + Shell
def greet(x):
return f"Hello {x}!"
echo @(greet('World'))
Defines a Python function and uses it with a shell command.