My personal bash prompt
Posted on 2017-07-18 (Updated on 2020-01-16)
Over years of using bash, I've customized my prompt configuration to fit my preferences. I value a minimal appearance, and I prefer to convey information with colors when possible. I also like it if information is hidden until relevant.
If that sounds like fun to you, you're in luck! Here's how I've done it. You can even get my latest
.bashrc file if you'd like.
Basic bash prompt information
The main file involved in your bash prompt is the bash configuration file,
.bashrc. This file is located in your home directory.
.bashrc file, there are three variables that affect your prompt:
PS1: This variable is your bash prompt. Any details or information that you want to show up on your prompt, you put in here.
PS2: The continuation prompt, which gets displayed if you are entering a multi-line command or statement.
PROMPT_COMMAND: This should be a bash function that will be run every time the prompt is displayed. Can be used to update variables used in the prompt, or even to set the
Bash allows you to set colors using escape sequences - special codes that are escaped by surrounding them with square brackets.
Bash prompts have several unique problems with colors and escape sequences. If you don't escape your color sequences at the right time, they can be escaped multiple times, which can wreak havoc with your terminal.
In addition, escape sequences are a little bit cryptic to look at. For instance, the escape sequence "
\E[37;44m" apparently colors your text white, and sets the background color to blue, according to TLDP.org's "colorizing scripts" tutorial.
To avoid all that, I use the
tput utility, I assign it directly to the PS1 variable, and only escape it at the last moment.
If you want your bash prompt to change based on different criteria, you can embed bash scripting into the variable with single quotes.
If you use single quotes, the text is only evaluated when your prompt is printed to the terminal. So,
PS1='$( date ) will make your prompt the date and time when the prompt was printed, and will be updated each printing.
I use embedded scripting to change the colors of different parts of my prompt, and add text in certain circumstances. For instance, if my current user has system mail in
/var/mail/[USER], my username is turned red and bold. When I delete that mail, it show up as orange again.
Here's an example of my prompt, and how I use colors and extra text to indicate important information about my system and my environment.