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

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)

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.

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

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.

vscode terminal usage tutorial vscode terminal usage tutorial Apr 15, 2025 pm 10:09 PM

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

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.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

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.

vscode terminal command cannot be used vscode terminal command cannot be used Apr 15, 2025 pm 10:03 PM

Causes and solutions for the VS Code terminal commands not available: The necessary tools are not installed (Windows: WSL; macOS: Xcode command line tools) Path configuration is wrong (add executable files to PATH environment variables) Permission issues (run VS Code as administrator) Firewall or proxy restrictions (check settings, unrestrictions) Terminal settings are incorrect (enable use of external terminals) VS Code installation is corrupt (reinstall or update) Terminal configuration is incompatible (try different terminal types or commands) Specific environment variables are missing (set necessary environment variables)