Some naughty operations on Linux Bash prompt

When you  open a Shell terminal in a Linux  environment   , you will see a Bash prompt similar to the following appearing on the command line: [user@$host ~]$ You know that the command line prompt can actually be set and added by yourself. Useful information? In this article, I will teach you how to customize your own Bash command line prompt. If you want to see it, keep reading~

How to set up the Bash prompt

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

[dneary@dhcp-41-137 ~]$ export PS1="[Linux Rulez]$ "

[Linux Rulez] export PS2="... "

[Linux Rulez] if true; then

... echo "Success!"

... fi

Success!

Where to set the value of PS1?

PS1 is an ordinary environment variable. The system default value is set in /etc/bashrc. In my system, the default prompt is set by the following command:

[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "

It determines whether PS1 is the system's default value \s-\v$, and if so, sets the value to [\u@\h \W]\$. (LCTT translation annotation: Note that the command is escaped with \.)

But if you want to customize the prompt, you should not modify /etc/bashrc. Instead, you should add custom commands to the .bashrc file in your home directory.

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

In the PROMPTING chapter of man bash, you can find descriptions of all PS1 and PS2 related special characters. Here are some of the more commonly used ones:

\u: username

\h: short hostname

\W: The name of the directory you are currently in (basename), ~ represents your home directory

\s: Shell name (bash or sh, depending on the name of your Shell)

\v: Shell version number

What other special strings can be used in prompts?

In addition to the above, there are many useful strings that can be used in prompts:

\d: Expand the date into the format "Tue Jun 27"

\D{fmt}: allows custom date format - see man strftime for more information

\D{%c}: Get localized date and time

\n: Line break (refer to the multi-line prompt below)

\w: displays the full path of the current working directory

\H: The full hostname of the current working machine

In addition to the above, you can also find more special characters and their uses 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 a complete date and time) and want to cut the prompt into two lines, you can use \n to cut the prompt into two lines for display, such as the following A multi-line example would display the date, time, and current working directory on the first line, and the username and hostname on the second line:

PS1="\D{%c} \w\n[\u@\H]$ "

Could it be any more fun?

People occasionally want to make prompts colorful. While I find colored prompts distracting and irritating, maybe you enjoy them. If we want to turn the date into red, the directory into cyan, and the username with 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]$ "

\[..\] : represents some non-printing characters

\e[..: Escape character, followed by a specific escape string to represent color or other meanings in the terminal

31m: indicates red font (41m indicates red background)

36m: indicates cyan font 1;

43m: indicates yellow font (1; 33m indicates yellow font)

[\e[0m]]: It restores the color to the system terminal default color at the end

You can find more color codes and even make characters invert and blink in the Bash prompt HOWTO here! I don’t know why people on earth think this way, but you can do it!

So what does your favorite custom prompt look like? Are there any custom prompts that drive you crazy? Please tell me in the comments~

Guess you like

Origin blog.csdn.net/yaxuan88521/article/details/133341653