Linux PS1 prompt

Source: Internet
Author: User
Tags printable characters

# PS1 = 'custom content' pay attention to the single quotation mark examples on both sides: PS1 = '(/u @/H/D/T)/$ 'your PS1: the user's usual prompt. PS2: the first line is not complete. Wait for the prompt entered in the second line. Linux system prompts are defined by the system variable ps1. The default format is [username @ host working directory] $. use echo $ PS1 to get the PS1 value, that is, PS1 = "[/u @/h/W]"/$ after Logging On, you can change the display style of ps1, however, after logging out and logging on to the system, the style becomes the default style of the system. To change the style completely, you can only change it from the configuration document. PS is defined in. bash_profile under the user root directory. For example, #. bash_profile # Get the aliases and functions if [-f ~ /. Bashrc]; then .~ /. Bashrc fi # user specific environment and startup programs # The following is the set PS1 value PS1 = "[/u @/h/W]/$" Path = $ path: $ home/bin # Use Export to output PS1 so that it can take effect in the sub-shell, this will cause the root user to also use this style # export PS1 should be careful to use the export path unset username below to briefly talk about the meaning of the default special symbols in the environment:/D: represents the date, the format is weekday month date, for example, "Mon Aug 1 region /h: complete host name. For example, if the name of my machine is fc4.linux, the name is fc4.linux/h: only the first name of the host. In the preceding example, the name is FC4 ,. linux is omitted/T: The display time is in the 24-hour format, for example, HH: mm: SS/T: The display time is in the 12-hour format/A: The display time is in the 24-hour format: hh: mm/u: Account name of the current user/V: Bash version/W: complete working directory name. In the home directory ~ Replace/W: Use basename to get the name of the working directory. Therefore, only the last directory/# is listed. The command/$ is the prompt character. If it is root, the prompt is: #. Common users are: $ ===================================================== ==============

We can make the prompt color by configuring the PS1 variable. In ps1, configure the Character Sequence color in the format of/[/E [F; BM/], where ''f'' is the font color, numbered from 30 ~ 37; ''B 'indicates the background color, numbered 40 ~ 47. You can use ''/E [0m'' to disable color output. In particular, when B is set to 1, the highlighted and bold text is displayed. For more information, see the following color table and code table. Foreground background color

---------------------------------------

30 40 black

31, 41 colors

32 42 colors

33, 43 colors

34 44 colors

35 45 purple

36 46 cyan

37 47 significance of white code

-------------------------

0 off 1 highlighted 4 underline 5 blinking 7 reversed white display 8 invisible

If you want to configure the terminal prompt style, set $ PS1 in ~ /. Bahrc specifies the ratio. For example, my configuration is as follows: configuration 1: PS1 = "/[/E [32 m/] [/u @/h/W] $/[/E [m/]" Export PS1 effect: [linuxsong @ test ~] $ Configuration 2: If [$ term = 'linux ']; then

Export PS1 = "[/[/E [36; 1 m/]/u @/h/[/E [31; 1 m/]/W/[/E [32; 1 m/]>"

Zhcon -- utf8

Clear

Elif [$ term = "xterm"]; then

Export PS1 = "[/[/E [31; 1 m/]/W/E [0 m]>"

FI =================================================== ====================

\ D: represents the date, in the format of weekday month date, for example: "Mon Aug 1"

\ H: complete host name. For example, if my machine name is fc4.linux, this name is fc4.linux.

\ H: only the first name of the host. In the preceding example, the name is FC4, and the name of. Linux is omitted.

\ T: The display time is in the 24-hour format, for example, HH: mm: Ss.

\ T: The display time is in the 12-hour format.

\ A: The display time is in the 24-hour format: hh: Mm.

\ U: Account name of the current user

\ V: Bash version information

\ W: complete working directory name. In the home directory ~ Replace

\ W: Use basename to get the working directory name. Therefore, only the last directory is listed.

#: Commands issued

\ $: The prompt character. If it is root, the prompt is: #. For common users, the prompt is: $ centos:

\ A an ASCII Bell character (07)

\ D the date in "weekday month date" format (e.g., "Tue may

26 ")

\ D {format} The format is passed to strftime (3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required

\ E an ASCII escape character (033)

\ H the hostname up to the First '.'

\ H the hostname

\ J the number of jobs currently managed by the Shell

\ L The basename of the shell's terminal device name

\ N newline

\ R carriage return

\ S the name of the shell, the basename of $0 (the portion

Following the final slash)

\ T the current time in 24-hour hh: mm: SS format

\ T the current time in 12-hour hh: mm: SS format

\ @ The current time in 12-hour AM/PM format

\ A the current time in 24-hour hh: Mm format

\ U the username of the current user

\ V The version of bash (e.g., 2.00)

\ V the release of bash, version + patch level (e.g., 2.00.0)

\ W the current working directory, with $ home abbreviated with a Tilde

\ W the basename of the current working directory, with $ home abbreviated with a Tilde

\! The history number of this command

\ # The command number of this command

\ $ If the specified tive uid is 0, a #, otherwise a $

\ NNN the character corresponding to the octal number NNN

\ A backslash

\ [Begin a sequence of non-printing characters, which cocould be used to embed a terminal control sequence into the prompt

\] End a sequence of non-printing characters

Therefore, you can specify your preferred command prompt format: PS1 = "\ U @ \ H: \ W \ $"

However, the prompt displayed after this setting has no color. If you want to add a color setting, you can do this: PS1 = "\ e [1; 32m \ U \ e [M \ e [1; 33m @ \ e [M \ e [1; 35m \ H \ e [M: \ W \ $"

At this point, the display in the terminal should be a color prompt, where \ e [1; xxm and \ e [m are the parts that change the terminal font color, the former is to change the color, the latter is restored to normal display. However, there is still a problem. When you enter the first line in the prompt, the line break of the carriage return is not normal, resulting in confusion. Therefore, in addition to this modification, you also need to include all non-printable characters in a dedicated bash escape sequence "\ [" and. The two sequences notify bash that the enclosed characters do not occupy any space on the row, so that the automatic line feed can continue to work normally. The final version is as follows (which can solve the problem of no line break with a color prompt): PS1 = "\ [\ e [1; 32m \] \ U \ [\ e [M \] \ [\ e [1; 33m \] @ \ [\ e [M \] \ [\ e [1; 35m \] \ H \ [\ e [M \]: \ W \ $"
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: info-contact@alibabacloud.com 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.