Some of the Sao actions of the Linux Bash prompt

Source: Internet
Author: User

When you open a Shell terminal in a Linux environment, you will see a Bash prompt similar to the following in the command line: [[email protected] $host ~]$ do you know that the command line prompt can actually be set to add many very useful information yourself? In this article I'll show you how to customize your own Bash command-line prompt and see what you want to see.

How to set up a Bash prompt

The Bash prompt is set through the environment variable PS1 (prompt string 1Prompt string 1), which is used for interactive shell prompts. Of course, if you need more input to complete a Bash command, the PS2 environment variable is used to set the multiline prompt:

[Email protected] ~]$ export ps1= "[Linux rulez]$" [Linux Rulez] Export ps2= "... "[Linux Rulez] if true; Then ... echo "success!" ... fisuccess!

Where do you set the value of PS1?

PS1 is a normal environment variable, the system default is set in/ETC/BASHRC, in my system, the default prompt is set by the following command:

["$PS 1" = "\\s-\\v\\\$"] && ps1= "[\[email protected]\h \w]\\$"

It determines whether PS1 is the default value of the system \s-\v$, and if so, sets the value to [\[email protected]\h \w]\$. (LCTT: Notice that the command was escaped.) )

But if you want to customize the prompt, you should not modify the/ETC/BASHRC, but instead add the custom command to the. bashrc file in your home directory.

What do the \u, \h, \w, \s and \v mentioned above mean?

In the prompting section of Man Bash, you can find descriptions of all the special characters associated with PS1 and PS2, and here are some of the more common ones:

\u: User name \h: short hostname \w: The name of the current directory you are in (basename), ~ indicates your home directory \s:shell name (bash or SH, depending on what your Shell name is) \v:shell version number

What other special strings can be used in the prompt

In addition to these, there are a number of useful strings that can be used in the prompt:

\d: Extend the date to "Tue June 27" This format \D{FMT}: Allow custom date format--can be obtained through man strftime for more information \d{%c}: Get localized date and Time \ n: NewLine (refer to the following multiline prompt) \ W: Displays the full path of the current working directory \h: The full hostname of the current working machine

In addition to these, you can find more special characters and their usefulness in the prompting section of the Bash man page.

Multi-line prompt

If your prompt is too long (for example, if you want to include \h, \w, or full datetime), to cut the prompt into two lines, you can use \ n to cut the prompt into two lines, such as the following multi-line example shows the date, time, and current working directory on the first line, and the second row displays the user name and hostname:

Ps1= "\d{%c} \w\n[\[email protected]\h]$"

Can we have some more fun?

People occasionally want to turn the prompt into color. Although I think the color prompt is distracting and irritable, you might like it. If we want to turn the date into red, the catalog turns blue, and the username has a yellow background, you can do this:

ps1= "\[\e[31m\]\d{%c}\[\e[0m\]\[\e[36m\]\w\[\e[0m\]\n[\[\e[1;43m\]\u\[\e[0m\]@\h]$" \[. \]: denotes some nonprinting characters \e[.: Escape character, followed by a specific escaped string in the terminal to represent a color or other meaning 31m: Represents a red font (41m is a red background) 36m: Indicates a blue font 1;43m: Represents a yellow font (1;33m indicates a yellow font) [\e[0m]: It finally restores the color to the system terminal default color

You can find more color codes here in Bash prompt HOWTO, and even allow characters to reverse and blink! I don't know why earth people have this idea, but you can do it!

So what does your favorite custom prompt look like? Do you have a custom prompt that makes you mad? Please tell me in the comments ~


Some of the Sao actions of the Linux Bash prompt

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.