In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the relevant knowledge of "how to customize the Ubuntu terminal prompt". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope this article "how to customize the Ubuntu terminal prompt" can help you solve the problem.
View the current Bash prompt configuration
When you open the terminal with the Dash or Ctrl+Alt + T shortcut keys, you will see the following prompt:
Linuxidc@linuxidc:~$
This default format lists the user name, hostname, and the user's current directory. Bash gets this configuration from the bashrc file, which is set separately for each user in their home directory. You can open this file like this:
Vim / .bashrc
Location: / home/username/.bashrc
You can also open this file using any other text editor.
This is what the configuration file looks like. We are here to show you the relevant parts of the file.
The PS1 variable you see in the image above has all the required configurations for the bash prompt. Let's first explain what the characters in this variable are. You can clearly see this variable in the PS1 variable listed after the else statement.
\ u-this indicates the user name of the current user
\ h: this represents the hostname of the current user extracted from the fully qualified domain name.
\ w: this represents the current working directory. The main catalogue is represented by a tilde ~ symbol.
\ $: this indicates whether you are a regular user ($) or a root user (#).
You can also view the configuration of the PS1 variable by echoing its value, as shown below:
Customize the Shell prompt
Now that you know where the prompt is stored and what the PS1 variable describes, let's take a look at how to edit it to customize the bash prompt.
Before editing the PS1 variable, it is important to store its default contents in a new variable. If an error occurs, this will help us restore the original configuration of the prompt. Enter the following command at the terminal:
$DEFAULT=$PS1
The variable "DEFAULT" now has all the information needed to restore the default prompt settings.
Now let's do some experiments with the PS1 variable. Enter the following command:
$PS1= "\ u\ $"
This new value of PS1 has the following effect on your prompt:
Now you can only see your user name and root user information, without any color, because no color information has been set yet.
Let's enter the following command so that the prompt also lists the working directory
$PS1= "\ u:\ w\ $"
This new value of PS1 has the following effect on your prompt:
Since my working directory is home, I can only see the ~ symbol. In other directories, such as www.linuxidc.com, my prompt displays the following information:
Set the prompt back to the default
Because we store the original configuration of the PS1 variable in the default variable, we can set the value of PS1 back to the default value by providing the value of the default variable to PS1.
PS1=DEFAULT
What else can you customize?
If you want the prompt to contain specific custom text, you can use the following syntax:
PS1= "[custom text]\ u@\ h:\ w\ $"
Example:
I have set up my prompt, including a custom message as follows:
Linuxidc@linuxidc:~$ PS1= "Linux Commune www.linuxidc.com\ u@\ h:\ w\ $"
Linux Commune www.linuxidc.com linuxidc@linuxidc:~$
You can add the following basic characters to the prompt:
Character usage\ d date format in terms of day and month. \ eASCII escape character\ an escape character. \ h the hostname of the current user up to the hostname of the current user. \ l the base name of the end device. \ j the number of jobs run by shell. \ r enter. \ nA new line. \ U the user name of the current user. \ vBash version. \! Prints the history number of the running command.
You can include the current system time in the prompt with the following command:
PS1= "\ A\ u:\ w\ $"
This new value of PS1 has the following effect on your prompt:
You can also customize the prompt to include the output of the command; this allows you to include unlimited options in the prompt.
Syntax:
$PS1= "\ u @\ h on `[command]`\ w\ $"
Example:
In this example, I will set the prompt to include the name of the primary group to which the current user belongs.
$PS1= "\ u @\ h on `id-gn`\ w\ $"
Shading Tip
After customizing the prompt, you will find that things may seem a little confusing. One obvious reason is that if they are all listed in the same color, it is not easy to distinguish one feature from another. Now let's learn how to color tips to make them more enjoyable.
To add colors to one or more features, the PS1 variable includes color markers. The highlighted text in the following image is color marked.
This is the format of the color marker:
\ [\ 033 [COLOR] m\]
For example, the default user name and hostname we see in the default terminal prompt are green because of the following color label:
\ [\ 033 [01bot 32m\]\ u@\ h\
You can change this color value for the bash prompt property to give it a new color, or add a new color marker to the property that has no color.
Here are some common colors and their values:
Color value Green32Red31Black30Blue34Cyan36Purple35Yellow33White37
Example:
The following command changes the prompt to red because we specified 31 (red) in the color marker:
$PS1= "\ [\ 033 [31m\]\ u@\ h:\ w$"
Use text styles in prompts
You can style the prompt text by specifying attribute values for the color tag. The following is the format of color tags with attributes:
\ [\ 033 [ATTRIBUTE; COLORm\]
You can use the following property values for prompt text:
Property value uses 0 standard text (this is the default even if the property is not set) 1 in the Ubuntu terminal, this value specifies bold text 2 dim text 4 text underline 5 flashing text 7 inverted text and background color 8 hidden text
Example:
You can underline the bash prompt using the following command:
$PS1= "\ [\ 033 [4bot 31m\]\ u@\ h:\ w$"
A value of 4 indicates that we want "underlined" text.
Make permanent changes to the prompt
Commands executed so far will only change the prompt for the current bash session. After you have tried text customization and prompt colorization, you need to edit the bashrc file.
Open the .bashrc file and copy the PS1 value you finally determined in the PS1 line under if; then line. In the following picture, I just changed the color of the bash prompt to red:
Press Ctrl+X to save the file, and then press y to save. Changes to the bash prompt will now be permanent. Exit the terminal and reopen it, and you can see that the bash prompt is still the same as you set.
Save Fil
That's all for "how to customize the Ubuntu terminal prompt". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.