Customize Bash Shell Prompt in Linux

Try this guide with our instant dedicated server for as low as 40 Euros

bash prompt

Ask Linux users and you will find that flexibility is one of the most important reasons they opted for the OS.

This flexibility is reflected in how you can customize various aspects of how the native and third-party utilities and tools look and function. 

Most work in Linux is done via a command interface or shell, also known as Bash (Bourne-Again Shell). Bash acts as a bridge between the user and the Linux operating system. Users interact with the system by providing commands to Bash. It interprets these commands and launches appropriate commands and utilities. 

A command prompt is typically the first thing you see when you open a Bash terminal window. This prompt indicates that the shell is prepared to handle the next command we run.

The bash command prompt has a preset format, often including important user- and system-specific variables, such as the working directory, hostname, and username.

This tutorial will look at how to personalize the various aspects of the Bash shell, including how the prompt looks and functions. 

Let’s start with a brief introduction to the Bash prompt.

Table Of Contents

  1. The Default Bash Prompt
  2. Linux Bash Prompt Customization
    1. The Bash Prompt Choices
    2. The Prerequisites
  3. Temporary Change of the Bash Prompt
  4. Modify the Linux Bash Prompt Permanently
  5. Frequently Used Custom Bash Prompt Options
    1. Example #1: Display the Domain Name and Username
    2. Example #2: Display Shell Name, Version, and Username Together
    3. Example #3: Insert the Time and Date in the Bash Prompt
    4. Example #4: Hide All Data in the Bash Prompt
    5. Example #5: Differentiate Between Root User and Regular User
  6. How to Change Bash Prompt Color
    1. The Popular Color Codes
  7. Reset Bash Changes to Default Settings
    1. Temporary Changes
    2. Permanent Changes
  8. Conclusion
  9. FAQs

The Default Bash Prompt

A Bash prompt is the text displayed in a terminal or command line interface, indicating that the shell is ready to accept commands. 

The default Bash prompt appears when a terminal or command line opens for the first time. When you open a terminal window, you’ll most likely see a prompt like the following:

username@hostname:~$

Or

(base) [username@localhost ~]$

Here,

username: Indicates the user currently logged in.

hostname: Indicates the name of your system.

@: Separates username and hostname.

~: Indicates that home is the currently selected directory.

$: Signifies a regular user with standard permissions.

#: Signifies a root user with administrative privileges. 

Linux Bash Prompt Customization

The default Bash prompt in Linux provides basic information about the currently set system variables. Similar to the majority of Linux programs, Bash acquires its default settings from a configuration file within the home directory. 

Located in your home directory, ~/.bashrc stores settings, including the prompt format. The name and path of the configuration file are as follows:

~/.bashrc

IMPORTANT: Make a backup copy of this configuration file before making any changes. This backup file is the restore point in case anything goes wrong.

Run the following cp command in the terminal to backup the Bash configuration file:

# cp ~/.bashrc ~/.bashrc.bak

This command copies and saves the configuration file to .bashrc.bak in the home directory.

The Bash Prompt Choices

Some of the most used Bash prompt choices are mentioned in the following table.

The Bash Prompt Choices

Note: Not all Linux distributions may support all of these options.

The Bash Environment Variables

The Bash prompt has four environment variables to control its appearance: 

  • PS1: Primary Prompt String 
  • PS2: Secondary Prompt String 
  • PS3: Select Command Prompt String
  • PS4: Debug Mode Prompt String

Let’s go into the details of these variables.

PS1

PS1 stands for Prompt Statement.

This main prompt is displayed in the terminal window where the commands are entered. It is the line that typically displays information such as username, hostname, and current working directory, along with a separator character like a dollar sign ($) or a colon (:).

Run the following command to see the current PS1 value:

# echo $PS1

For the default settings, the terminal may return similar to the image below:

echo $PS1

PS2 

PS2 helps you differentiate between the actual command prompt (PS1) and the continuation prompt when entering a lengthy command.

To display the current PS2 value, run the following command:

# echo $PS2

The system would display an angle bracket:

>

By default, PS2 is usually just an angle bracket (>), indicating the continuation of a multi-line command.

PS3 

PS3 is the select command prompt. It is typically blank by default used for interactive menus created with the select built-in command.

PS4 

PS4 is used internally for debugging shell scripts and might contain a plus sign (+) or other information, depending on the context.

The Prerequisites

Before diving into how to customize the Bash prompt, ensure you have the following:

  • A system running a mainstream Linux distribution
  • Access to the terminal or command line interface
  • An account with root or sudo privileges

Temporary Change of the Bash Prompt

You can temporarily modify the values for the Bash with the export command. Note that these changes will remain in effect only for the duration of the current session. As a result, the shell will revert to defaults in the next session.

For instance, you can temporarily limit the Bash prompt to displaying the username only with the following command:

# export PS1="\u >"

The prompt would immediately change to display only the username in the following format:

username >

username

You can revert to the default prompt display by signing out and relogging with the same user credentials. 

Modify the Linux Bash Prompt Permanently

You can permanently modify the Linux Bash prompt with these steps.

Step #1: Edit the .bashrc file

Open the Bash configuration file in your preferred text editor. We will use Nano with the following command:

# sudo nano ~/.bashrc

Enter your password when prompted.

Step #2: Locate the PS1 variable

You will notice multiple statements for various settings in this file. 

Some of them are descriptions of the statements  the Bash interpreter considers as comments. These lines in blue are preceded by a # sign. The lines in white are actual statements that affect the appearance and operation of the shell. 

Look for lines containing PS1. This variable defines the prompt format.

Step #3: Customize the Prompt

Go to the end of the configuration file. Add the following statement:

PS1="MyTestPrompt> "

myTestPrompt

You can replace “MyTestPrompt> ” with any text string you like. 

Step #4: Save and Exit

Press Ctrl+O to save the file and Ctrl+X to exit the editor.

Step #5: Reload the Configuration

You need to restart the Bash service to make your modifications effective. We recommend using the source command to reload the configuration file without closing the shell. 

# source ~/.bashrc

The output will be similar to the following screengrab:

source ~.bashrc

Frequently Used Custom Bash Prompt Options

There are several ways to personalize your Bash prompt using escape sequences, which are special characters that insert information or format the looks of the prompt string. 

You can use these escape sequences in two ways: permanently modify the values in the ~/.bashrc file or temporarily use the export command.

We will now demonstrate five scenarios that show how you can customize the Bash prompt.

Example #1: Display the Domain Name and Username

Use the –H flag with the export command to display your username and the complete hostname (including the domain name). The command format in this context will be as follows: 

# export PS1="\u\H "

export PS1

Example #2: Display Shell Name, Version, and Username Together

If you wish to display the username, shell name, and Bash version in the prompt, run the following command format:

# export PS1="\u >\s\v "

U indicates username.

S indicates the shell name (here, BASH). 

V indicates the shell version (here, 5.0).

The output would be in the following format:

username >bash5.0

username bash5.0

Example #3: Insert the Time and Date in the Bash Prompt

Bash offers extensive options for displaying the date and time in multiple formats. Some popular flags include the following:

  • d: Displays the current date in [weekday]/[month]/[day]
  • t: Displays the time in a 24-hour format.
  • T: Displays the current time in a 12-hour format.
  • A: Displays the current time in only hours and minutes using a 24-hour format.

For instance, run the following command to display just the date:

# export PS1="\u@\H>\d "

Similarly, the following command displays the current time in 24-hour format:

# export PS1="\u@\H>\t "

Alternatively, run the following command to display time in a 12-hour format:

# export PS1="\u@\H>\T "

The following command format shows the time in hours and minutes:

# export PS1="\u@\H>\A "

Example #4: Hide All Data in the Bash Prompt

If you prefer a minimal prompt that doesn’t display your username or hostname, we recommend the following command format:

# export PS1="\W > "

The output would be this minimal prompt:

~ >

This prompt hides both the username and hostname, leaving only the current working directory in the prompt.

Example #5: Differentiate Between Root User and Regular User

By default, the Bash prompt uses different symbols to indicate user type:

  • $: Regular user with standard permissions.
  • #: Root user with administrative privileges.

For instance, in the following command,

# export PS1="\u@\H \W:\$ "

$ denotes the current user is not a root user.

Note: We don’t recommend additional formatting to indicate this distinction, as the system handles it automatically.

How to Change Bash Prompt Color

Adding colors to your Bash prompt is a great way to personalize and improve readability. Changing the font colors is an important aspect of Bash prompt modifications.

For instance, to change the text of your Bash prompt purple, run the following command:

# export PS1="\e[0;35m[\u@\h \W]\$ \e[0m"

Here,

  • \e[: Changes the color
  • 0;35m: Specifies the color code
  • [\u@\h \W]\$: The code for your standard Bash prompt (username@hostname, working directory, and user type)
  • \e[0m: Resets the color to default after the prompt information

When executed, The command will change the font color to purple without affecting the text in the prompt.

export PS1 2

The Popular Color Codes

The color code has two parts that control the typeface and the color of the prompt. 

The initial number in the color code indicates the typeface:

  • 0: Normal (default)
  • 1: Bold (bright)
  • 2: Dim
  • 4: Underlined

The second number indicates the foreground color:

  • 30: Black
  • 31: Red
  • 32: Green
  • 33: Brown
  • 34: Blue
  • 35: Purple
  • 36: Cyan
  • 37: Light gray

Background Colors: Use codes from 40 to 47 (similar to foreground colors, but less commonly used in prompts).

Bright Colors: Add 1; before the foreground color code to create a brighter version of that color.

Reset Bash Changes to Default Settings

There are two methods for reversing the changes to the Bash prompt. 

Temporary Changes

If you have made temporary changes using the export PS1=”” command, simply logging out of your current session will revert the changes.

This is because the export command only affects the current shell environment. Once you log out and log back in, a fresh shell session starts with the default prompt settings.

Permanent Changes

If you made permanent modifications to the \.bashrc file, you can restore the default settings using one of two methods:

Method #1: Comment Out Changes

Follow these steps for commenting out specific statements:

  • Open the .bashrc file in a text editor (e.g., nano ~/.bashrc).
  • Find the lines you added to modify the prompt.
  • Add the # symbol at the beginning of each line. This converts those lines into comments, effectively disabling them without deleting the code.
  • Save the file and exit the editor.
  • Source the .bashrc file to apply the changes immediately. The command in this scenario is as follows: 

# source ~/.bashrc

Method #2: Restore From the Backup 

If you created a backup of your original .bashrc file before making changes, run the following to restore your backup’s default settings.

# sudo cp ~/.bashrc.bak ~/.bashrc

Conclusion

You can modify the look and feel of your command line interface on Linux by modifying your Bash prompt. By changing the Bash prompt, you can customize your preferences and workflow. This modification can involve inserting custom text or symbols, displaying the current directory, displaying system statistics, or adding colors. 

FAQs

Q. How do I change my Bash prompt in Linux?

You can change your Bash prompt by modifying the PS1 environment variable in your Bash configuration file (usually ~/.bashrc or ~/.bash_profile). You can customize it by adding special escape sequences for colors, system information, current directory, etc.

Q. What are some common customizations for the Bash prompt?

Common customizations include adding colors to differentiate between different types of information, displaying the username, hostname, current directory, git branch (if in a git repository), and adding special symbols or emojis for visual flair.

Q. Can I use variables or commands in my Bash prompt?

Yes, you can use variables and commands in your Bash prompt. For example, you can include the output of commands like date or pwd to display dynamic information in your prompt.

Q. How can I revert to the default Bash prompt?

To revert to the default Bash prompt, comment or remove any customizations you’ve made in your Bash configuration file (e.g., ~/.bashrc or ~/.bash_profile). Alternatively, you can set the PS1 variable back to its default value.

Q. Will changing my Bash prompt affect my system’s performance?

No, changing your Bash prompt should not significantly impact your system’s performance. The prompt is a relatively simple text string displayed each time a new shell session is opened. However, complex customizations involving dynamic information or frequent updates may have minimal performance effects.

Q. Can I use special characters or emojis in my Bash prompt?

You can use special characters and emojis in your Bash prompt to add visual elements or symbols. However, ensure your terminal emulator supports these characters to avoid rendering issues.

Q. Can different prompts for different users or hosts be possible?

You can customize the Bash prompt differently for different users or hosts by adding conditional statements in your Bash configuration file. This allows you to have personalized prompts based on user accounts or specific machines.

Q. Are tools or scripts available to help with Bash prompt customization?

Yes, several online tools and scripts can assist with Bash prompt customization. These tools often provide predefined prompt styles or offer interactive interfaces to create custom prompts tailored to your preferences.

Q. Can I share my custom Bash prompt with others?

You can share your custom Bash prompt configurations with others by providing the relevant snippets from your Bash configuration file (e.g., ~/.bashrc or ~/.bash_profile). You can also create and distribute custom prompt themes or scripts for others.

Q. Will changing my Bash prompt affect the behavior of other shell commands or scripts?

No, changing your Bash prompt should not affect the behavior of other shell commands or scripts. The prompt is purely a visual element and does not impact the functionality or execution of other commands or scripts run in the shell.

 

Try this guide with our instant dedicated server for as low as 40 Euros