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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

How to run python with notepad How to run python with notepad Apr 16, 2025 pm 07:33 PM

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

How to run sublime code python How to run sublime code python Apr 16, 2025 am 08:48 AM

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

See all articles