How to Customize Bash Command Prompt in Linux

How to Customize Bash Command Prompt in Linux

Preface

As we all know, bash (the B ourne-A gain Shell) 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 customize some basic display methods manually without using plug-ins and tools, such as adding or modifying certain elements, changing the foreground color, changing the background color, etc.

Customize bash command prompt in Linux

In bash, we can customize the bash command prompt by changing the value of the $PS1 environment variable.

Typically, the bash command prompt will look like this:


In the default display above, "sk" is my username and "ubuntuserver" is my hostname.

Just insert some special escape sequences that begin with a backslash and you can modify the command prompt to your liking. Let me give you a few examples.

Before you begin, I strongly recommend that you back up your ~/.bashrc file.

$ cp ~/.bashrc ~/.bashrc.bak

Change the username@hostname portion of the bash command prompt

As shown above, the bash command prompt generally has a "username@hostname" part, which can be modified.

Just edit the ~/.bashrc file:

$ vi ~/.bashrc

Add a line at the end of the file:

PS1="ostechnix> "

Replace "ostechnix" with any word you want, then press ESC and enter :wq to save and exit the file.

Execute the following command to make the changes take effect:

$ source ~/.bashrc

You can see the "ostechnix" you just added appears in the bash command prompt.


Let's look at another example, such as replacing "username@hostname" with "Hello@welcome>".

Modify the ~/.bashrc file as before.

export PS1=Hello@welcome>

Then execute source ~/.bashrc to make the changes take effect immediately.

The following are the results after I modified them on Ubuntu 18.04 LTS.


Show username only

If you need to display only the username, just add the following line to your ~/.bashrc file.

export PS1="\u "

The \u here is an escape string.

The following are some escape strings that you can add to the $PS1 environment variable to change the style of the bash command prompt. After each modification, you need to execute the source ~/.bashrc command to make it take effect immediately.

Display username and hostname

export PS1="\u\h "

The command prompt will look like this:

skubuntuserver

Display username and fully qualified domain name

export PS1="\u\H "

Display additional characters between username and hostname

If you need to display other characters (such as @) between the username and hostname, you can use the following format:

export PS1="\u@\h "

The command prompt will look like this:

sk@ubuntuserver

Display username, hostname, and add $ sign at the end

export PS1="\u@\h\\$ "

Combining the above two display methods

export PS1="\u@\h> "

The command prompt will eventually look like this:

sk@ubuntuserver>

Similarly, you can add other special characters such as colon, semicolon, asterisk, underscore, space, etc.

Display username, hostname, shell name

export PS1="\u@\h>\s "

Display username, hostname, shell name, and shell version

export PS1="\u@\h>\s\v "

bash command prompt display style:


Display username, host name, current directory

export PS1="\u@\h\w "

If the current directory is $HOME, it is shown with a tilde ( ~ ).

Display date in bash command prompt

If you want to display the date in your bash command prompt in addition to your username and hostname, you can add the following to your ~/.bashrc file:

export PS1="\u@\h>\d "

Display date and 12-hour time in 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 time in 24-hour format hh:mm:ss

export PS1="\u@\h>\d\t "

Above are some common escape sequences that can change the bash command prompt. For other escape sequences, see the PROMPTING chapter of the bash man page.

You can also execute the following command at any time to view the current command prompt style.

$ echo $PS1

Remove the username@hostname part in the bash command prompt

If I don't want to make any adjustments, can I just remove the username@hostname part entirely? The answer is yes.

If you are a technical blogger, you may need to upload your Linux terminal screenshots on your website or blog. Maybe your username and hostname are too cool and unique and you don’t want others to see them. In this case, you need to hide the “username@hostname” part in the command prompt.

If you don't want to expose your username and hostname, just follow the steps below.

Edit ~/.bashrc file:

$ vi ~/.bashrc

Add this line at the end of the file:

PS1="\W> "

Enter :wq to save and close the file.

Execute the following command to make the changes take effect immediately.

$ source ~/.bashrc

Now look at your terminal and the “username@hostname” part is gone, leaving only a ~> sign.


If you want to keep things as simple as possible and don't want to mess with your ~/.bashrc file, the best thing to do is to create another user on the system (eg "user@example", "admin@demo"). When using a user with such a command prompt to take screenshots or record the screen, you don't need to worry about your username or host name being seen by others.

Warning: In some cases, this practice is not recommended. For example, shells like zsh will inherit the settings of the current shell, which may cause some unexpected problems. This trick is only used to hide the "username@hostname" part of the command prompt, nothing more, and if you use this trick for other purposes, abnormalities may occur.

Colorize the bash command prompt

Currently we have only changed the content in the bash command prompt. The following will introduce how to color the command prompt.

By writing some configuration to the ~/.bashrc file, you can modify the foreground color (that is, the color of the text) and background color of the bash command prompt.

For example, the following line of configuration can turn the color of some text into red:

export PS1="\u@\[\e[31m\]\h\[\e[m\] "

After adding the configuration, execute source ~/.bashrc to take effect immediately.

Your bash command prompt will look like this:


Similarly, you can change the background color with this configuration:

export PS1="\u@\[\e[31;46m\]\h\[\e[m\] "

Add emoji

Everyone loves emojis. You can also insert emoji into the command prompt as follows.

PS1="\W

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support for 123WORDPRESS.COM.

You may also be interested in:
  • Some cool operations of Linux Bash prompt (custom Bash prompt)
  • How to configure the display format of the Linux command prompt
  • 30 interesting commands and tips in Linux shell (recommended)
  • Starship's custom shell prompt enables information freedom

<<:  mysql creates root users and ordinary users and modify and delete functions

>>:  Vue scaffolding learning project creation method

Recommend

Linux RabbitMQ cluster construction process diagram

1. Overall steps At the beginning, we introduced ...

HTML Tutorial: Unordered List

<br />Original text: http://andymao.com/andy...

XHTML Web Page Tutorial

<br />This article is mainly to let beginner...

How to clear the timer elegantly in Vue

Table of contents Preface optimization Derivative...

Learn about TypeScript data types in one article

Table of contents Basic Types any type Arrays Tup...

Nginx Service Quick Start Tutorial

Table of contents 1. Introduction to Nginx 1. Wha...

Two simple menu navigation bar examples

Menu bar example 1: Copy code The code is as foll...

Node.js+express message board function implementation example

Table of contents Message Board Required librarie...

How to use Linux tr command

01. Command Overview The tr command can replace, ...

Detailed explanation of various loop speed tests in JS that you don’t know

Table of contents Preface 1. for loop 2. while lo...

Several ways to add timestamps in MySQL tables

Scenario: The data in a table needs to be synchro...

The latest popular script Autojs source code sharing

Today I will share with you a source code contain...