Modify the command prompt and its colors for Linux

Source: Internet
Author: User

We use the Linux system when the most contact is its command-line window, many times we need to enter commands on the command line, before the input command will have a prompt, the general system default prompt form is:

[[Email protected] working directory

In fact, we can customize the command line prompt for ourselves, the Linux system prompt is defined by the system variable PS1, as long as the value of the PS1 system variable is modified by the rules arbitrarily modify the command line prompt.
From the command Echo $PS 1 View the value of the PS1, which is ps1= "[\[email protected]\h: \w \t]\$". After you modify the value of PS1, the value of PS1 changes back to the default value after you restart the system or log back in. If you want to permanently save the PS1 value to the user, you can re-import the new value of the system variable PS1 by modifying the user profile. Profiles.

Command prompt default is $PS1, note : "P,s" is uppercase

[[email protected] ~]# echo $PS1[\[email protected]\h \W]\$

In addition to the above default symbols, the following special symbols represent the meaning of

\d: #代表日期, formatted as weekday month date, for example: "Mon-1"
\h: #完整的主机名称
\h: #仅取主机的第一个名字
\ t: #显示时间为24小时格式, such as: HH:MM:SS
\ t: #显示时间为12小时格式
\a: #显示时间为24小时格式: hh:mm
\u: #当前用户的账号名称
\v: #BASH的版本信息
\w: #完整的工作目录名称
\w: #利用basename取得工作目录名称, so only the last directory is listed
#: #下达的第几个命令
$: #提示字符, if it is root, the prompt is: #, the average user is: $

For example, give the command a multibyte 24-hour format and add color to the prompts.
Note : The format for setting character sequence colors in PS1 is: [\e[f; Bm "] where" F "is the font color, number is 30-37," B "is the background color, number 40-47.

Next color.

The prompt turns purple and the output font remains the same, depending on your preferences.
Color Information table

Font background Color
30 40 Black
31 41 Red
32 42 Green
33 43 xxx
34 44 Blue
35 45 Purplish red
36 46 Cyan Blue
37 47 White

No invalidation after setting reboot

The PS1 command can set the command prompt for the current shell, which is a function in the shell, but the shell is also a program that has a process life cycle that will keep the data lost in memory as the process life cycle ends, and if you want to save the configuration, you need to save the assignment in a file. There are two types of shell environment configuration files:

Profile class: Provides configuration for the interactive login shell, interactive login to log in to open shell process directly through a terminal input account password, or use Su-username to perform the login switch; his reading order is:/etc/profile-->/etc/ Profile.d/*-->/bash.profile-->/.bahrc-->/etc/bashrc
BASHRC class: Provides configuration for non-interactive login shell, non-interactive user login switch performed for SU username, read order: ~/.BASHRC-->/ETC/BASHRC-->/etc/profile.d/

If you want to set the terminal prompt style as long as you specify $PS1 in ~/.BAHRC, my settings are as follows:

Export PS1


The Export command is used to output a shell variable as an environment variable, or to output a shell function as an environment variable.

When a variable is created, it is not automatically known to the shell process that was created after it. The command export can pass the value of the variable to the following shell. When a shell script is called and executed, it does not automatically get access to the variables defined in the script (the caller) unless the variables have been explicitly set to available. The export command can be used to pass the value of one or more variables to any subsequent script.


Modify the command prompt and its colors for Linux

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.