Customizing your command line prompt

Have you ever wanted to customize your Linux command line prompt, but found yourself lost? Perhaps you did a Google search, found that the PS1 variable in your bash profile controls how your command line prompt looks, then tracked this down in your .bash_profile:

“e” followed by unclosed brackets?? What sort of language is this? Well, welcome to BASH.

BASH, however illogical it may seem, actually follows a very logical (albeit weird) syntax. If we deconstruct the syntax and master it, then we can learn to harness the full power of BASH. Which in this case, means creating arbitrary and awesome command line prompts!

I thought the most helpful way to explain how this is to retrace how I came to understand the PS1 variable and along with it, Bash colors.

First, I took a long hard look at the PS1 string, and started to play around with it. After a bit of experimenting, cutting out parts of the PS1 and reloading my bash profile to see the effects, I realized that there were a few essential parts of the PS1 variable:

1) Repeating escape characters

Within the original PS1 variable (copied again below), you can see that there is the repeating set of characters “[\[\e”. This stands for a set of two escape characters in BASH. Before stating a color preference, it’s absolutely necessary that you first insert an escape character so that BASH knows that you are not declaring a literal string.

\[ = begin a sequence of non printing characters

\e[ = start a color scheme

\] = end a color scheme

Combine the following two and you get “\[\e[” a sequence which you will see repeating many times throughout the PS1 variable and which I lumped together as, for convenience:

local START=”\[\e[”

local END=”\]”

Now lets take a look at how our PS1 variable looks after abstracting away this sequence of escape characters:

Ah, a little bit better! You may be wondering why the first sequence “\[\e]” didn’t get commented out… turns out those first 14 characters were not impacting the visual output of the PS1 anyway, so we’re going to remove them, bringing us to:

Now, what is the next pattern you can see here? A bunch of numbers separated by ; and followed by m. What’s that?

Well, by adjusting each of these numbers and seeing the effects (and Googling), we learn that this semicolon-separated syntax is a BASH way of declaring colors.

2) Colors

What’s interesting here is that sometimes you have just the number followed by m, sometimes you have 0; then the number followed by m, and lastly you even have x;y;zm. What do each of these mean?

For the rest of this walk through, we are going to stick to the vanilla color format of the first example above, so that we can stay sane and get the fundamentals down.

Let’s take a look at where we last left off.

Now we understand how to interpret the number sequences as color declarations. To award ourselves greater control, lets abstract away these colors and store them as convenient local variables. Since we see “0m,” or white, several times, we will store white separately for readability. Also, the $RED variable as defined in our template bash profile actually has the START and END characters included within them, so we redefined. See

Wow! What we have now almost reads like English. Start from left to right and you can read,

“First a dangling [. Then, declare (start, specify, end) the time color, and insert “\t”. Then, declare white, and a dangling ]. Then declare the git color, and make a call to the “parse_git_branch” function. Then, declare the directory color, and insert “\W”. Then, declare white, add a new line (“\n”), and then declare the character color, followed by the character. Lastly, declare white.”

3) PS1 Variables

At this point, the only parts we haven’t fully understood are “\W” and “\t”. I found this resource to be helpful. We learn the following.

“\t” = The time, in 24-hour HH:MM:SS format.

“\W” = The basename of $PWD.

You can also include any ASCII character. Here’s a list:

Synthesizing all of this, we can draw some conclusions about how BASH colors and PS1 work together. BASH colors remind me a lot of variable scope. In order to have a color impact something you have to open up a “color gate” after which anything you type will be in precisely that color. The only way to close a “color gate” is to declare a new color. That’s why you see in our PS1 variable that we end with a “white” color declaration — that’s so that any text you type in your Terminal shows up as white! If you want all of your text to show up in, say hot pink, feel free to change that last color declaration.

4) A template PS1 for you

As you get ready to customize your PS1, I recommend storing a handy set of colors and then flexibly point our color selections to these colors. Here’s an example of the PS1 section of my .bash_profile looks:

I hope this has been helpful. As Avi said on Day 2, command line is the workbench of a programmer. You ought to customize it to suit your preferences.

Go forth and customize your command line prompt!


Like this:

Like Loading…

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.