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:
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: 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. 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. To display the current date and time using the PS1 variable in Bash, run the following command with the escape sequences: 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. 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: Now, to change the background color, run the following command in your terminal: 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: 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. 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: 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. 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: To customize the values of the PS1 variable permanently in bash, follow the steps below: 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: 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. 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. 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. Save the configuration file after modifying the PS1 variable and restart your terminal or run Yes, PS1 supports variables and command substitution to display dynamic information. For example, To reset the prompt’s text and color formatting, append the escape sequence Yes, there are more prompt-related variables like PS2, PS3, and PS4. Yes, it is possible to share the custom prompt configurations by sharing the shell configuration files with others. << Go Back to Types of Variables in Bash | Bash Variables | Bash Scripting Tutorial
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?
echo PS1
3 Customization Examples of Bash PS1 Variable
1. Display the Date and Time in the Bash Prompt
export PS1="\u@\h [\$(date '+%Y-%m-%d %H:%M:%S')] \w \$ "
2. Change the Background and Foreground Color of Bash Prompt
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
export PS1="\[\e[44m\]\u@\h \w \$ \[\e[m\]"
export PS1="\[\e[97m\]\u@\h \w \$ \[\e[m\]"
3. Display Emoji in the Bash Prompt
export PS1="\u@\h \w 😎 \$ "
What’s So Special About Bash PS1 Variable?
How to Customize the PS1 Values Permanently in Bash Command Prompt
~/.bashrc
file using a text editor by running the command below:
sudo nano ~/.bashrc
#Displaying current date and time
PS1="\u@\h [\$(date '+%Y-%m-%d %H:%M:%S')] \w \$ "
source ~/.bashrc
Conclusion
People Also Ask
What do you mean by escape sequences in the PS1 variable in Bash?
How to make my custom PS1 persistent?
source ~/.bashrc
to make the custom PS1 persistent.Does PS1 support variables and command substitution?
$(command)
holds the output of a command, /h
specifies the hostname, etc.How to reset the prompt’s text and color formatting?
\[\e[m\]
.Are there any variables related to prompt except PS1?
Is it possible to share the custom prompt configurations?
Related Articles
FUNDAMENTALS
A Complete Guide for Beginners