Home Common Problem What is prompt in linux

What is prompt in linux

Mar 07, 2023 am 10:10 AM
linux prompt

prompt是指终端提示符(Shell提示符),是在linux操作系统中,提示进行命令输入的一种工作提示符。对于普通用户,Base shell默认的提示符是美元符号“$”;对于超级用户(root用户),Bash Shell默认的提示符是井号“#”;该符号表示Shell等待输入命令。

What is prompt in linux

本教程操作环境:centos8系统、Dell G3电脑。

prompt是指终端提示符,是在linux操作系统中,提示进行命令输入的一种工作提示符。

启动终端模拟包或者从 Linux 控制台登录后,便可以看到 Shell 提示符。提示符是通往 Shell 的大门,是输入 Shell 命令的地方。

对于普通用户,Base shell 默认的提示符是美元符号$;对于超级用户(root 用户),Bash Shell 默认的提示符是井号#。该符号表示 Shell 等待输入命令。

不同的 Linux 发行版使用的提示符格式不同。例如在 CentOS 中,默认的提示符格式为:

[mozhiyan@localhost ~]$
Copy after login

这种格式包含了以下三个方面的信息:

  • 启动 Shell 的用户名,也即 mozhiyan;

  • 本地主机名称,也即 localhost;

  • 当前目录,波浪号~是主目录的简写表示法。

Shell 通过PS1和PS2两个环境变量来控制提示符格式:

  • PS1 控制最外层命令行的提示符格式。

  • PS2 控制第二层命令行的提示符格式。

在 Shell 中初次输入命令,使用的是 PS1 指定的提示符格式;如果输入一个命令后还需要输入附加信息,Shell 就使用 PS2 指定的提示符格式。请看下面的例子:

[mozhiyan@localhost ~]$ echo "PHP中文网"
PHP中文网
[mozhiyan@localhost ~]$ echo "https://www.php.cn/"
https://www.php.cn/
[mozhiyan@localhost ~]$ echo "
> yan
> chang
> sheng
> "
yan
chang
sheng
[mozhiyan@localhost ~]$
Copy after login

echo 是一个输出命令,可以用来输出数字、变量、字符串等;本例中,我们使用 echo 来输出字符串。

字符串是一组由" "包围起来的字符序列,echo 将第一个"作为字符串的开端,将第二个"作为字符串的结尾。此处的字符串就可以看做 echo 命令的附加信息。

本例中,前两次使用 echo 命令时都是在后面紧跟字符串,一行之内输入了完整的附加信息。第三次使用 echo 时,将字符串分成多行,echo 遇到第一个"认为是不完整的附加信息,所以会继续等待用户输入,直到遇见第二个"。输入的附加信息就是第二层命令,所以使用>作为提示符。

要显示提示符的当前格式,可以使用 echo 输出 PS1 和 PS2:

[mozhiyan@localhost ~]$ echo $PS1
[\u@\h \W]\$
[mozhiyan@localhost ~]$ echo $PS2
>
[mozhiyan@localhost ~]$
Copy after login

Shell 使用以\为前导的特殊字符来表示命令提示符中包含的要素,这使得 PS1 和 PS2 的格式看起来可能有点奇怪。下表展示了可以在 PS1 和 PS2 中使用的特殊字符。

Bash shell 提示符可以包含的要素
字符描述
\a铃声字符
\d格式为“日 月 年”的日期
\eASCII转义字符
\h本地主机名
\H完全合格的限定域主机名
\jshell当前管理的作业数
\1shell终端设备名的基本名称
\nASCII换行字符
\rASCII回车
\sshell的名称
\t格式为“小时:分钟:秒”的24小时制的当前时间
\T格式为“小时:分钟:秒”的12小时制的当前时间
\@格式为am/pm的12小时制的当前时间
\u当前用户的用户名
\vbash shell的版本
\Vbash shell的发布级别
\w当前工作目录
\W当前工作目录的基本名称
\!该命令的bash shell历史数
\#该命令的命令数量
\$如果是普通用户,则为美元符号$;如果超级用户(root 用户),则为井号#
\nnn对应于八进制值 nnn 的字符
\\斜杠
\[控制码序列的开头
\]控制码序列的结尾

注意,所有的特殊字符均以反斜杠\开头,目的是与普通字符区分开来。您可以在命令提示符中使用以上任何特殊字符的组合。

我们可以通过修改 PS1 变量来修改提示符格式,例如:

[mozhiyan@localhost ~]$ PS1="[\t][\u]\$ "
[17:27:34][mozhiyan]$
Copy after login

新的 Shell 提示符现在可以显示当前的时间和用户名。不过这个新定义的 PS1 变量只在当前 Shell 会话期间有效,再次启动 Shell 时将重新使用默认的提示符格式。

相关推荐:《Linux视频教程

The above is the detailed content of What is prompt in linux. 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 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months 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 start apache How to start apache Apr 13, 2025 pm 01:06 PM

The steps to start Apache are as follows: Install Apache (command: sudo apt-get install apache2 or download it from the official website) Start Apache (Linux: sudo systemctl start apache2; Windows: Right-click the "Apache2.4" service and select "Start") Check whether it has been started (Linux: sudo systemctl status apache2; Windows: Check the status of the "Apache2.4" service in the service manager) Enable boot automatically (optional, Linux: sudo systemctl

What to do if the apache80 port is occupied What to do if the apache80 port is occupied Apr 13, 2025 pm 01:24 PM

When the Apache 80 port is occupied, the solution is as follows: find out the process that occupies the port and close it. Check the firewall settings to make sure Apache is not blocked. If the above method does not work, please reconfigure Apache to use a different port. Restart the Apache service.

How to monitor Nginx SSL performance on Debian How to monitor Nginx SSL performance on Debian Apr 12, 2025 pm 10:18 PM

This article describes how to effectively monitor the SSL performance of Nginx servers on Debian systems. We will use NginxExporter to export Nginx status data to Prometheus and then visually display it through Grafana. Step 1: Configuring Nginx First, we need to enable the stub_status module in the Nginx configuration file to obtain the status information of Nginx. Add the following snippet in your Nginx configuration file (usually located in /etc/nginx/nginx.conf or its include file): location/nginx_status{stub_status

How to set up a recycling bin in Debian system How to set up a recycling bin in Debian system Apr 12, 2025 pm 10:51 PM

This article introduces two methods of configuring a recycling bin in a Debian system: a graphical interface and a command line. Method 1: Use the Nautilus graphical interface to open the file manager: Find and start the Nautilus file manager (usually called "File") in the desktop or application menu. Find the Recycle Bin: Look for the Recycle Bin folder in the left navigation bar. If it is not found, try clicking "Other Location" or "Computer" to search. Configure Recycle Bin properties: Right-click "Recycle Bin" and select "Properties". In the Properties window, you can adjust the following settings: Maximum Size: Limit the disk space available in the Recycle Bin. Retention time: Set the preservation before the file is automatically deleted in the recycling bin

How to restart the apache server How to restart the apache server Apr 13, 2025 pm 01:12 PM

To restart the Apache server, follow these steps: Linux/macOS: Run sudo systemctl restart apache2. Windows: Run net stop Apache2.4 and then net start Apache2.4. Run netstat -a | findstr 80 to check the server status.

The importance of Debian Sniffer in network monitoring The importance of Debian Sniffer in network monitoring Apr 12, 2025 pm 11:03 PM

Although the search results do not directly mention "DebianSniffer" and its specific application in network monitoring, we can infer that "Sniffer" refers to a network packet capture analysis tool, and its application in the Debian system is not essentially different from other Linux distributions. Network monitoring is crucial to maintaining network stability and optimizing performance, and packet capture analysis tools play a key role. The following explains the important role of network monitoring tools (such as Sniffer running in Debian systems): The value of network monitoring tools: Fast fault location: Real-time monitoring of network metrics, such as bandwidth usage, latency, packet loss rate, etc., which can quickly identify the root cause of network failures and shorten the troubleshooting time.

How to optimize the performance of debian readdir How to optimize the performance of debian readdir Apr 13, 2025 am 08:48 AM

In Debian systems, readdir system calls are used to read directory contents. If its performance is not good, try the following optimization strategy: Simplify the number of directory files: Split large directories into multiple small directories as much as possible, reducing the number of items processed per readdir call. Enable directory content caching: build a cache mechanism, update the cache regularly or when directory content changes, and reduce frequent calls to readdir. Memory caches (such as Memcached or Redis) or local caches (such as files or databases) can be considered. Adopt efficient data structure: If you implement directory traversal by yourself, select more efficient data structures (such as hash tables instead of linear search) to store and access directory information

How to solve the problem that apache cannot be started How to solve the problem that apache cannot be started Apr 13, 2025 pm 01:21 PM

Apache cannot start because the following reasons may be: Configuration file syntax error. Conflict with other application ports. Permissions issue. Out of memory. Process deadlock. Daemon failure. SELinux permissions issues. Firewall problem. Software conflict.