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!
Key Takeaways
- Learning about Bash PS1 variable.
- Learning about the special factors of PS1 variable in Bash.
- Exploring some practical examples of Bash PS1 variable.
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 Commonly Used Backslash-escape Characters in Bash
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 command prompt string. Here are some commonly used backslash-escape characters 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: i) Username (\u), ii) Hostname (\h), iii) Full path of the current working directory (\w). 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 save the values of the PS1 variable permanently, follow the steps below: Steps to Follow > ➊ Open the ‘~/.bashrc’ file using a text editor by running the command 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: ➌ Click on CTRL+S to save the file and CTRL+X to exit. ➍ Now, to make the changes reset in your current terminal, run the command: 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. Related Articles << Go Back to Types of Variables in Bash | Bash Variables | Bash Scripting TutorialBackslash-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
Example 1: Displaying the Date and Time in the Bash Prompt
export PS1="\u@\h [\$(date '+%Y-%m-%d %H:%M:%S')] \w \$ "
Example 2: Changing 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\]"
Example 3: Displaying 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
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