What is PS1 Variable in Bash? [3 Customization Examples]

In Bash, the PS1 variable plays a pivotal role in shaping and enhancing the command line experience. This is such a powerful variable that helps to customize the format and appearance of the primary Bash prompt. In this article, you will find detailed ideas about the Bash PS1 variable regarding its specialty, customization format, etc. So, let’s explore!

What is PS1 Variable in Bash?

PS1 variable is a type of environment variable that defines the generation and customization of the content of the primary prompt that appears while interacting with the Bash shell. Actually, PS1 stands for Prompt String 1. It specifies how the prompt appears before you type a new command in your terminal. Moreover, you can modify this PS1 variable and customize the prompt’s appearance.

Some Backslash-escape Characters Used in PS1 Bash Variable

In Bash, the backslash-escape sequences are used to represent the special behavior of specific characters. And you can use these backslash-escape characters to customize the PS1 Bash variable. Here are some commonly used backslash-escape characters used in PS1 variable in Bash:

Backslash-escaped Character Description
\u It displays the current username.
\h It represents the short hostname of the system.
\H It represents the full hostname of the system including the domain.
\d It represents the current date format like Day Month Date.
\w It displays the full path of the current working directory.
\W It displays the last part of the current working directory.
\t It represents the current time in 24-hour format (Hour:Minute:Second).
\T It represents the current time in a 12-hour format with AM/PM (Hour:Minute:Second AM/PM).
\@ It represents the current time in a 12-hour format with AM/PM (Hour:Minute AM/PM).
\e It represents the ASCII escape character used to control & format sequences.
\n It specifies a newline character.
\v It displays only the release version of the Bash.
\V It displays the release version & patch level of the Bash.

How to Check the Default Value of Bash PS1 Variable?

You can simply open your Ubuntu terminal, run the echo command and check the default value of the PS1 variable in Bash. Basically, for the Ubuntu-based system, the default value of PS1 includes three information:

  1. Username (\u),
  2. Hostname (\h),
  3. Full path of the current working directory (\w).
echo PS1
EXPLANATION
  • echo PS1: It displays the default value of the PS1 variable.

Checking the default values of PS1

From the image, you can see the default value of the PS1 variable and the format of the primary prompt that appeared in the terminal.

3 Customization Examples of Bash PS1 Variable

You can use the Bash PS1 variable for different temporary customization purposes lasted for the current shell. Here, in the following section, I am demonstrating three examples of the PS1 variable in Bash.

The default command prompt will appear when you’ll reopen the terminal. So, you can read this section to save the values of the PS1 variable permanently.

1. Display the Date and Time in the Bash Prompt

To display the current date and time using the PS1 variable in Bash, run the following command with the escape sequences:

export PS1="\u@\h [\$(date '+%Y-%m-%d %H:%M:%S')] \w \$ "
EXPLANATION
  • export PS1: It temporarily sets the values of the PS1 variable for the current shell session.
  • \u: It represents the username of the current user.
  • \h: It represents the hostname of the device.
  • [\$(date ‘+%Y-%m-%d %H:%M:%S’)]: The […] syntax contains the current date and time. And the \$(…) syntax inside the […] allows to include the output of the executed command in the prompt. Here, I have executed the date command with the format ‘+%Y-%m-%d %H:%M:%S’ which resembles the year, month, day, hour, minute, second.
  • \w: It symbolizes the full path of the current working directory.
  • \$: It indicates the end of the prompt.

Displaying date and time using customized PS1 variable

In the above image, I have run the export command with the customized value of the PS1 variable. Hence, you can see nadiba as the Username, Ubuntu as the Hostname, 2023-08-20 as the current Date, 01:45:17 as the current Time, and ~/Desktop as the full path of the current Working Directory when I executed the command.

2. Change the Background and Foreground Color of Bash Prompt

You can use different color codes with the escape character and change the background and foreground colors of the command prompt. Have a look at the following list of some basic ANSII escape codes for background and foreground color output:

Color Name Background Color Code Foreground Color Code
Dark Gray 40 30
Red 41 31
Green 42 32
Yellow 43 33
Blue 44 34
Purple 45 35
Cyan 46 36
Light Gray 47 37
White 107 97

Now, to change the background color, run the following command in your terminal:

export PS1="\[\e[44m\]\u@\h \w \$ \[\e[m\]"
EXPLANATION
  • \[\e[44m\]: It sets the background color to Blue (44). Here, the character m is used to set the sequence.
  • \u@\h: It displays the username & the hostname.
  • \w: It displays the full path of the current working directory.
  • \$: It indicates the end of the prompt.
  • \[\e[m\]: It resets the color formatting to the default.

Changing the background color to 'Blue'

From the above image, you can see that I have changed the background color of the Bash prompt to Blue by using the accurate color code 44.

Similarly, to change the foreground color, run the command below:

export PS1="\[\e[97m\]\u@\h \w \$ \[\e[m\]"
EXPLANATION
  • \[\e[97m\]: It sets the foreground color to White (97). Here, the character m is used to set the sequence.

Changing the foreground color to 'White'

From the above image, you can see that I have changed the foreground color of the Bash prompt to White by using the accurate color code 97.

3. Display Emoji in the Bash Prompt

If you want to display emoji characters in your command prompt, you need to copy & paste the specific Unicode escape sequences for different emojis. By using the bytes value of the emoji character, you can incorporate these sequences into your PS1 configuration and add a glimpse of fun to your command prompt.

Let’s run the following command to display the emoji in the Bash command prompt:

export PS1="\u@\h \w 😎 \$ "
EXPLANATION
  • 😎: It represents the Unicode character for the cool face emoji.

Displaying emoji in command prompt

From the above image, you can see that I have added a cool face emoji to my Bash prompt. You can choose and display any emoji of your choice from different websites that provide Unicode characters.

What’s So Special About Bash PS1 Variable?

In Bash, the PS1 variable is so special as it controls the format and behavior of the primary prompt of the shell. Here are the facts that make the PS1 variable so special:

  • Smooth Interaction: As a user, you can interact with your shell in the primary prompt provided by the PS1 variable. This prompt provides essential information and confirms a smooth user interaction.
  • Customization: The PS1 variable allows you to customize your command line experience more effectively by tailoring the primary prompt to display information according to your needs and preferences.
  • Dynamic Contents: Displaying dynamic elements is another special factor of the Bash PS1 variable. By using the escape sequences and command substitutions, you can add dynamic content to your prompt.
  • Shell Scripting: The PS1 variable plays a crucial role in shell scripting too. During shell scripting, the modification of the PS1 variable helps to convey necessary prompts and alerts to the users.

How to Customize the PS1 Values Permanently in Bash Command Prompt

To customize the values of the PS1 variable permanently in bash, follow the steps below:

  1. Open the ~/.bashrc file using a text editor by running the command below:
    sudo nano ~/.bashrc
    EXPLANATION
    • sudo: Super User DO.
    • nano: A text editor.
    • ~/.bashrc: The path to the user-specific configuration file for the Bash shell.

    Opening '.bashrc' file in Nano text editor

  2. Now, scroll down and write the customized PS1 values you want to display at the end of the file. Here, I have mentioned an example for displaying the current date and time permanently in the Bash prompt:

    #Displaying current date and time
    PS1="\u@\h [\$(date '+%Y-%m-%d %H:%M:%S')] \w \$ "

    Setting the PS1 values in '.bashrc' file

  3. Click on CTRL+S to save the file and CTRL+X to exit.
  4. Now, to make the changes reset in your current terminal, run the command:
    source ~/.bashrc
    EXPLANATION
    • source: It executes the contents of a script file.
    • ~/.bashrc: The path to the user-specific configuration file for the Bash shell.

    Setting the changed command prompt permanent

    After all the procedures, you’ll see that the default command prompt has been changed and whenever you open a new terminal, the changes made in the prompt will remain permanent.

Conclusion

Wrapping up the whole article, whether you are a regular Linux user or not, by exploring the customizing capabilities of the PS1 variable with different configurations, you can personalize and optimize your Bash prompt and alters your command-line interactions into a more enjoyable experience.

People Also Ask

What do you mean by escape sequences in the PS1 variable in Bash?

Escape sequences refer to the special characters that direct the terminal to perform some particular operations, such as including username, hostname, changing text color, etc.

How to make my custom PS1 persistent?

Save the configuration file after modifying the PS1 variable and restart your terminal or run source ~/.bashrc to make the custom PS1 persistent.

Does PS1 support variables and command substitution?

Yes, PS1 supports variables and command substitution to display dynamic information. For example, $(command) holds the output of a command, /h specifies the hostname, etc.

How to reset the prompt’s text and color formatting?

To reset the prompt’s text and color formatting, append the escape sequence \[\e[m\].

Are there any variables related to prompt except PS1?

Yes, there are more prompt-related variables like PS2, PS3, and PS4.

Is it possible to share the custom prompt configurations?

Yes, it is possible to share the custom prompt configurations by sharing the shell configuration files with others.


Related Articles


<< Go Back to Types of Variables in Bash | Bash Variables | Bash Scripting Tutorial

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
icon linux

Nadiba Rahman

Hello, This is Nadiba Rahman, currently working as a Linux Content Developer Executive at SOFTEKO. I have completed my graduation with a bachelor’s degree in Electronics & Telecommunication Engineering from Rajshahi University of Engineering & Technology (RUET).I am quite passionate about crafting. I really adore exploring and learning new things which always helps me to think transparently. And this curiosity led me to pursue knowledge about Linux. My goal is to portray Linux-based practical problems and share them with you. Read Full Bio

Leave a Comment