Home Operation and Maintenance Linux Operation and Maintenance What does prompt refer to in linux?

What does prompt refer to in linux?

May 27, 2023 pm 04:49 PM
linux prompt

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

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 "本站"
本站
[mozhiyan@localhost ~]$ echo "https://www.yisu.com/"
https://www.yisu.com/
[mozhiyan@localhost ~]$ echo "
> yan
> chang
> sheng
> "
yan
chang
sheng
[mozhiyan@localhost ~]$
Copy after login

echo is a command for printing that outputs numbers, variables, strings, etc. In this example, we are using echo to print a string.。

字符串是一组由" "包围起来的字符序列,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 时将重新使用默认的提示符格式。

The above is the detailed content of What does prompt refer to 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

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 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)

Hot Topics

Java Tutorial
1667
14
PHP Tutorial
1273
29
C# Tutorial
1255
24
Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

vscode Previous Next Shortcut Key vscode Previous Next Shortcut Key Apr 15, 2025 pm 10:51 PM

VS Code One-step/Next step shortcut key usage: One-step (backward): Windows/Linux: Ctrl ←; macOS: Cmd ←Next step (forward): Windows/Linux: Ctrl →; macOS: Cmd →

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

How to run sublime after writing the code How to run sublime after writing the code Apr 16, 2025 am 08:51 AM

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

laravel installation code laravel installation code Apr 18, 2025 pm 12:30 PM

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

git software installation git software installation Apr 17, 2025 am 11:57 AM

Installing Git software includes the following steps: Download the installation package and run the installation package to verify the installation configuration Git installation Git Bash (Windows only)

See all articles