Table of Contents
Getting Started
Using Python
Using Bash
Xonsh handles Bash statements as single lines. This means:
Using Python in Bash
Using Bash with Python
Question
Summary
Home Backend Development Python Tutorial Share a powerful shell language and command prompt based on Python

Share a powerful shell language and command prompt based on Python

Apr 11, 2023 pm 07:49 PM
python Order shell

Share a powerful shell language and command prompt based on Python

Xonsh is a Python-based, cross-platform, Unix-oriented shell language and command prompt. It is essentially a Python interpreter with good syntax for running Shell commands. . So you have all the power of Python in a familiar shell environment. Works on all major systems including Linux, OSX and Windows.

Bash scripts are fast and efficient for small or batch applications. One of the limitations of Bash is its handling of mathematical functions and floating point numbers.

If you are a Python programmer, you will like it very much. It has a huge library of available functions.

For Raspberry Pi users, Xonsh can provide many opportunities to write some extremely streamlined scripts. Python can be used to connect to third-party devices and sensors.

In this article, I will introduce Xonsh through some examples.

Getting Started

See the Xonsh documentation for system-specific installation instructions. To install on Raspberry Pi/Ubuntu/Debian, enter:

linuxmi@linuxmi:~/www.linuxmi.com$ sudo apt install xonsh
Copy after login

Share a powerful shell language and command prompt based on Python

To run Xonsh, just enter: xonsh

Share a powerful shell language and command prompt based on Python

Out-of-the-box Xonsh provides a configuration wizard and a tutorial.

Using Python

Python code can be entered directly on the command line. The version of Python will depend on what is loaded on the underlying system. To check your version:

linuxmi@linuxmi ~/www.linuxmi.com $ import sys
linuxmi@linuxmi ~/www.linuxmi.com $ sys.version
'3.9.7 (default, Sep 10 2021, 14:59:43) n[GCC 11.2.0]'
Copy after login

Share a powerful shell language and command prompt based on Python

As with the interactive Python interface, no print statements are required to view the output:

linuxmi@linuxmi ~/www.linuxmi.com $ 7+9
16
linuxmi@linuxmi ~/www.linuxmi.com $ a=5;b=6
linuxmi@linuxmi ~/www.linuxmi.com $ a+b
11
linuxmi@linuxmi ~/www.linuxmi.com $ i="Hello World!"
linuxmi@linuxmi ~/www.linuxmi.com $ i + " www.linuxmi.com"
'Hello World! www.linuxmi.com'
Copy after login

Share a powerful shell language and command prompt based on Python

Using Bash

Xonsh uses Python first, so take ls (Bash list command) as an example:

In the above example ***ls*** is used first as Bash list command, but if a variable with the same name is defined, that variable is referenced.

linuxmi@linuxmi ~/www.linuxmi.com $ ls
'VS Code.png' www.linuxmi.com.jpg www.linuxmi.com.py wwww.linuxmi.com
 linuxmi.comwww.linuxmi.com.mp4 www.linuxmi.png
linuxmi@linuxmi ~/www.linuxmi.com $ ls="这是一个变量www.linuxmi.com"
linuxmi@linuxmi ~/www.linuxmi.com $ ls
'这是一个变量www.linuxmi.com'
Copy after login

Share a powerful shell language and command prompt based on Python

The first ls is used as the Bash list command, and the second one is the Xonsh display variable ls.

Xonsh handles Bash statements as single lines. This means:

  • Bash for/while/if statements need to be all on one line
  • Remember to use spaces between characters
  • Line expansion (with "") is not supported
  • Bash functions are not supported.
  • You can write functions in Python instead of Bash

Using Python in Bash

Python statements are used in Bash: @( Python statements). Here are two examples of using Python with Bash:

linuxmi@linuxmi ~/www.linuxmi.com $ import sys
linuxmi@linuxmi ~/www.linuxmi.com $ echo @(sys.version)
3.9.7 (default, Sep 10 2021, 14:59:43)
[GCC 11.2.0]
linuxmi@linuxmi ~/www.linuxmi.com $ echo @("LinuxMi.com=" +str(8+9))
LinuxMi.com=17
Copy after login

Share a powerful shell language and command prompt based on Python

Examples

linuxmi@linuxmi ~/www.linuxmi.com $ from tkinter import *
linuxmi@linuxmi ~/www.linuxmi.com $ root = Tk()
linuxmi@linuxmi ~/www.linuxmi.com $ canvas = Canvas(root, width = 500, height =
500)
linuxmi@linuxmi ~/www.linuxmi.com $ canvas.pack()
linuxmi@linuxmi ~/www.linuxmi.com $ img = PhotoImage(file="www.linuxmi.com.png")
linuxmi@linuxmi ~/www.linuxmi.com $ canvas.create_image(0,0, anchor=NW, image=im
g)
1
linuxmi@linuxmi ~/www.linuxmi.com $ mainloop()
Copy after login

Share a powerful shell language and command prompt based on Python

Using Bash with Python

Bash variables can be used directly in Python, for example:

Use Bash date and pass it to Python

linuxmi@linuxmi ~/www.linuxmi.com $ now=$(date)
linuxmi@linuxmi ~/www.linuxmi.com $ print("现在的时间是: " + now)
现在的时间是: 2022年 04月 01日 星期五 19:17:13 CST
Copy after login

Share a powerful shell language and command prompt based on Python

Question

Working between different shells and subshells can be a bit confusing. I find that I sometimes get confused about which shell I'm working in. The ps command will tell me if xonsh* is running:

linuxmi@linuxmi ~/www.linuxmi.com $ ps
PID TTYTIME CMD
 7966 pts/100:00:00 bash
 8044 pts/100:00:04 xonsh
11342 pts/100:00:00 ps
Copy after login

Share a powerful shell language and command prompt based on Python

I am able to pass the Xonsh script to the Bash script without any problems, but I find that for some operation, I need to kill the Xonsh shell manually.

Summary

Xonsh is suitable for everyday use by experts and novices alike. Unlike other shells, xonsh is based on Python, adding additional syntax that makes it easy to invoke subprocess commands, operate the environment, and work with file systems. The xonsh command prompt provides users with interactive access to the xonsh language.

Xonsh has a lot of potential for users looking for a simple scripting solution.

For myself, I would probably stick with a Bash or Python solution, but I have other options.

The above is the detailed content of Share a powerful shell language and command prompt based on Python. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use Debian Apache logs to improve website performance How to use Debian Apache logs to improve website performance Apr 12, 2025 pm 11:36 PM

This article will explain how to improve website performance by analyzing Apache logs under the Debian system. 1. Log Analysis Basics Apache log records the detailed information of all HTTP requests, including IP address, timestamp, request URL, HTTP method and response code. In Debian systems, these logs are usually located in the /var/log/apache2/access.log and /var/log/apache2/error.log directories. Understanding the log structure is the first step in effective analysis. 2. Log analysis tool You can use a variety of tools to analyze Apache logs: Command line tools: grep, awk, sed and other command line tools.

Python: Games, GUIs, and More Python: Games, GUIs, and More Apr 13, 2025 am 12:14 AM

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

How debian readdir integrates with other tools How debian readdir integrates with other tools Apr 13, 2025 am 09:42 AM

The readdir function in the Debian system is a system call used to read directory contents and is often used in C programming. This article will explain how to integrate readdir with other tools to enhance its functionality. Method 1: Combining C language program and pipeline First, write a C program to call the readdir function and output the result: #include#include#include#includeintmain(intargc,char*argv[]){DIR*dir;structdirent*entry;if(argc!=2){

The role of Debian Sniffer in DDoS attack detection The role of Debian Sniffer in DDoS attack detection Apr 12, 2025 pm 10:42 PM

This article discusses the DDoS attack detection method. Although no direct application case of "DebianSniffer" was found, the following methods can be used for DDoS attack detection: Effective DDoS attack detection technology: Detection based on traffic analysis: identifying DDoS attacks by monitoring abnormal patterns of network traffic, such as sudden traffic growth, surge in connections on specific ports, etc. This can be achieved using a variety of tools, including but not limited to professional network monitoring systems and custom scripts. For example, Python scripts combined with pyshark and colorama libraries can monitor network traffic in real time and issue alerts. Detection based on statistical analysis: By analyzing statistical characteristics of network traffic, such as data

Python and Time: Making the Most of Your Study Time Python and Time: Making the Most of Your Study Time Apr 14, 2025 am 12:02 AM

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Nginx SSL Certificate Update Debian Tutorial Nginx SSL Certificate Update Debian Tutorial Apr 13, 2025 am 07:21 AM

This article will guide you on how to update your NginxSSL certificate on your Debian system. Step 1: Install Certbot First, make sure your system has certbot and python3-certbot-nginx packages installed. If not installed, please execute the following command: sudoapt-getupdatesudoapt-getinstallcertbotpython3-certbot-nginx Step 2: Obtain and configure the certificate Use the certbot command to obtain the Let'sEncrypt certificate and configure Nginx: sudocertbot--nginx Follow the prompts to select

How to configure HTTPS server in Debian OpenSSL How to configure HTTPS server in Debian OpenSSL Apr 13, 2025 am 11:03 AM

Configuring an HTTPS server on a Debian system involves several steps, including installing the necessary software, generating an SSL certificate, and configuring a web server (such as Apache or Nginx) to use an SSL certificate. Here is a basic guide, assuming you are using an ApacheWeb server. 1. Install the necessary software First, make sure your system is up to date and install Apache and OpenSSL: sudoaptupdatesudoaptupgradesudoaptinsta

See all articles