


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
To run Xonsh, just enter: xonsh
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]'
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'
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'
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
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()
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
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
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!

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.

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.

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function