Shell Programming in Python
Snake Shell
Create lightweight Raspberry Pi scripts with Xonsh, a Python shell that lets you write scripts in Python with Bash commands mixed in.
For Raspberry Pi users, Xonsh [1] offers many opportunities to write some extremely lightweight scripts, with Python connecting to physical devices and Bash utilities accessing system and file resources.
In this article, we look at two lean Xonsh projects for the Raspberry Pi. The first program connects to a DHT11 temperature and humidity sensor and shows the results in a Bash dialog in just five lines. The second project calls the Bash top utility in a lean eight lines to show the Raspberry Pi idle time and user time on a 16x2 LCD screen.
Getting Started
Xonsh has two requirements: Python 3.8 or greater and a Bash shell. To install and run Xonsh on a Raspberry Pi, Ubuntu, or Debian system, enter:
sudo apt install xonsh xonsh
When the Xonsh shell opens, the terminal label changes so you can see that you are working in Xonsh rather than a standard Bash shell (Figure 1). When the shell first starts up, two options are presented: xonfig tutorial, which opens a browser help window, and xonfig web, which allows users to tweek the xonsh shell configuration.
Within the Xonsh shell, you can enter both Bash and Python statements, such as:
$ # Mix Bash and Python Lines $ echo "Time is:" $(date +%T) ; # Bash Time is: 12:59:51 $ print("%d + %d = %d" % (2,3,2+3)) ; # Python 2 + 3 = 5
Xonsh in the Terminal
The Xonsh shell is designed for Python users, so some handy features are available from the command prompt. The first of these features is code highlighting. Xonsh highlights comments, strings, functions, and command statements (Figure 2).
Xonsh also handles Python indentation errors for single-line statements and non-control statements. Figure 3 shows an example of Python code that successfully runs with both overindented and underindented statements.
The next useful feature is Python help (Figure 4). The Tab key offers a dropdown dialog of Python objects or methods from the typed string. The arrow keys let you select the required help options within the dialog.
Bash Zenity Dialogs with Python
Python has some great graphic libraries such as Tkinter, Qt, and PySimpleGUI. These libraries are excellent for complex GUI applications, but they can be overkill if you only need a simple dialog.
The Zenity utility [2] is a quick way to present a variety of dialogs in just one line of Bash code. Zenity is preloaded on Raspberry Pi OS and most Linux installations, so no added installation is usually required.
The basic syntax for a Zenity information dialog is:
$ zenity --info --title=myTitle --text=myMessage
In Xonsh, Python statements and variables are used directly in a Bash statement as:
@(<Python statement>)
For example, the Bash echo
command can print a Python statement:
$ import sys $ echo "OS: " @(sys.platform) OS: linux
These two steps can be combined to show the OS in a Zenity dialog:
$ import sys $ zenity --info --title=System_OS --text=@(sys.platform)
Once you have these two concepts working (Zenity and Python embedded in Bash), you can start creating some quick and easy Raspberry Pi applications.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Gnome 48 Debuts New Audio Player
To date, the audio player found within the Gnome desktop has been meh at best, but with the upcoming release that all changes.
-
Plasma 6.3 Ready for Public Beta Testing
Plasma 6.3 will ship with KDE Gear 24.12.1 and KDE Frameworks 6.10, along with some new and exciting features.
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.
-
HashiCorp Cofounder Unveils Ghostty, a Linux Terminal App
Ghostty is a new Linux terminal app that's fast, feature-rich, and offers a platform-native GUI while remaining cross-platform.
-
Fedora Asahi Remix 41 Available for Apple Silicon
If you have an Apple Silicon Mac and you're hoping to install Fedora, you're in luck because the latest release supports the M1 and M2 chips.
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.