In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Preface
As we all know, bash (the B ourne- A gain Sh ell) is the default shell used by most Linux distributions. This article will show you how to customize the display of the bash command prompt by adding colors and styles. Although many plug-ins or tools can easily meet this requirement, we can also manually customize some basic display methods without using plug-ins and tools. for example, add or modify some elements, change the foreground color, change the background color, and so on.
Customize the bash command prompt in Linux
In bash, we can customize the bash command prompt by changing the value of the $PS1 environment variable.
In general, the bash command prompt would take the following form:
In the default display above, "sk" is my user name and "ubuntuserver" is my hostname.
Just insert some special escape strings that start with a backslash and you can modify the command prompt as you like. Let me give you a few examples.
Before you begin, I strongly recommend that you back up ~ / .bashrc files in advance.
$cp ~ / .bashrc ~ / .bashrc.bak
Change the username@hostname part of the bash command prompt
As shown above, the bash command prompt usually has a "username@hostname" section, which can be modified.
You only need to edit ~ / .bashrc file:
$vi ~ / .bashrc
Add a line at the end of the file:
PS1= "ostechnix >"
Replace the above "ostechnix" with any word you want to use, then press ESC and type: wq to save and exit the file.
Execute the following command to make the previous changes take effect:
$source ~ / .bashrc
You can see the "ostechnix" you just added in the bash command prompt.
Let's look at another example, such as replacing "username@hostname" with "Hello@welcome >".
Also modify the ~ / .bashrc file as just now.
Export PS1=Hello@welcome >
Then execute source ~ / .bashrc to make the changes take effect immediately.
The following is the effect of my modification on Ubuntu 18.04 LTS.
Show only user name
If you need to display only the user name, just add the following line to the ~ / .bashrc file.
Export PS1= "\ u"
The\ u here is an escaped string.
Here are some escape strings that can be added to the $PS1 environment variable to change the style of the bash command prompt. After each change, you need to execute the source ~ / .bashrc command to take effect immediately.
Show user name and hostname
Export PS1= "\ u\ h"
The command prompt is displayed as follows:
Skubuntuserver
Show user name and fully qualified domain name
Export PS1= "\ u\ H"
Display other characters between user name and host name
If you need to display other characters (such as @) between the user name and the host name, you can use the following format:
Export PS1= "\ u @\ h"
The command prompt is displayed as follows:
Sk@ubuntuserver
Display the user name, hostname, and add the $symbol at the end
Export PS1= "\ u @\ h\\ $"
Combine the above two display modes
Export PS1= "\ u @\ h >"
The command prompt ends up like this:
Sk@ubuntuserver >
Similarly, you can add other special characters, such as colons, semicolons, asterisks, underscores, spaces, and so on.
Show user name, hostname, shell name
Export PS1= "\ u @\ h >\ s"
Displays the user name, hostname, shell name, and shell version
Export PS1= "\ u @\ h >\ s\ v"
Bash command prompt display style:
Show user name, hostname, current directory
Export PS1= "\ u @\ h\ w"
If the current directory is $HOME, it will be displayed with a wavy line (~).
Display the date in the bash command prompt
In addition to the user name and hostname, if you want to display the date in the bash command prompt, you can add the following to the ~ / .bashrc file:
Export PS1= "\ u @\ h >\ d"
Display the date and 12-hour time in the bash command prompt
Export PS1= "\ u@\ h >\ d\ @"
Display date and time in hh:mm:ss format
Export PS1= "\ u@\ h >\ d\ T"
Display date and 24-hour time
Export PS1= "\ u@\ h >\ d\ A"
Display date and 24-hour hh:mm:ss format time
Export PS1= "\ u @\ h >\ d\ t"
These are some common escape strings that can change the bash command prompt. Other escape strings can be found in the PROMPTING section of the man manual of bash.
You can also execute the following command at any time to view the current command prompt style.
$echo $PS1
Remove the username@hostname section from the bash command prompt
If I don't want to make any adjustments, can I just get rid of the whole username@hostname part? The answer is yes.
If you are a technical blogger, you may need to upload screenshots of your Linux terminals on your website or blog. Maybe your username and hostname are too cool and alternative to be seen, in which case you need to hide the "username@hostname" section of the command prompt.
If you don't want to expose your user name and hostname, just follow these steps.
Edit ~ / .bashrc file:
$vi ~ / .bashrc
Add this line at the end of the file:
PS1= "\ W >"
Enter: wq saves and closes the file.
Execute the following command to make the changes take effect immediately.
$source ~ / .bashrc
Now take a look at your terminal, the "username@hostname" section has disappeared, leaving only a ~ > tag.
If you want to operate as easily as possible without messing up your ~ / .bashrc file, the best way is to create another user in the system (such as "user@example", "admin@demo"). If you take a screenshot or screenshot with such a command prompt, you don't have to worry about your user name or host name being seen by others.
Warning: in some cases, this practice is not recommended. For example, shell such as zsh inherits the current shell settings, and some unexpected problems may arise at this time. This technique is only used to hide the "username@hostname" part of the command prompt, that's all. If you use this technique for other purposes, you may get an exception.
Shading the bash command prompt
So far, we have only changed the contents of the bash command prompt. Here's how to color the command prompt.
You can change the foreground color (that is, the color of the text) and background color of the bash command prompt by writing some configurations to the ~ /. Bashrc file.
For example, the following line is configured to make some text red:
Export PS1= "\ u @\ [\ e [31m\]\ h\ [\ e [m\]"
After adding the configuration, the execution of source ~ / .bashrc takes effect immediately.
Your bash command prompt will look like this:
Similarly, you can use this configuration to change the background color:
Export PS1= "\ u @\ [\ e [31 [46m\]]\ h\ [\ e [m\]"
Add emoji
Everybody likes emoji. You can also insert emoji into a command prompt in the following configuration.
PS1= "\ W
Summary
The above is the whole content of this article, I hope that the content of this article has a certain reference and learning value for your study or work, if you have any questions, you can leave a message and exchange, thank you for your support.
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: 278
*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.