Changing Your Shell Prompt

By | November 22, 2023

This post may contain affiliate links. Please read my disclosure for more info.

I’m still setting up my Linux systems at work, and one of the things that I like to change is the BASH shell’s prompt. I’ve found when I’m rolling back through shell commands to see the output of past commands, there’s not a good way to separate one command from another with the standard prompt. You end up straining to see where one command started as you scroll back through history.

What I like to do is customize the output of the prompt, and change the color to make it stand out. Let’s do that.

The prompt variable is one of many variables in the BASH shell. It’s name is PS1, and you can change it simply by assigning it a new value. What were going to do is play a little, before we do the actual change. If you mess up, just re-start your shell, and you’ll have the original prompt back.

First, let’s see what the current PS1 variable looks like. At the shell prompt, type, not forgetting the $ in front, echo $PS1 and then return. You should get a long character string. This will make sense to you shortly.

Let’s play, type, this time without the $, keeping the spaces between the colon.

PS1=”This is my new Prompt : “

Hit the return a couple of times to see that indeed it is your new prompt. If you mess up and want to start over, just restart the shell. The spaces were picked up between the colon to give you a nicer prompt.

Now let’s get to business. I like to have my my current user name (\u), which you may change occasionally in a session, and the current directory I’m in (\w) shown in my prompt. The space between the \u and \w gives you a space in your prompt. Let’s do that, type;

PS1=” [\u \w] $ “

After a couple of returns we see our prompt has changed. The spaces we put in between the quotes mattered. “Dale” is my current user name. The tilde, ~ , represents my home directory, which in this case is, /home/Dale.

All other directories are shown as is. Let’s change your directory, to show you this.

cd /

which takes you to the root directory, then

cd etc

Your prompt now changes to reflect your directory.

There are many options you can use to change your prompt other than \u or \w. Here’s a few more.

\u   the current user
\w   the current directory
\d   the date, like so: “Wed Dec 20”
\h   the hostname
\s   the name of the shell
\t   current time in 24 hour format
\T   current time in 12 hour format
\!   the history number of this command

There are a few more, but these are the main ones you’ll want to consider. For a complete list see the BASH manual, under6.9 Controlling the Prompt“.

The last thing we’ll do is change the color of the prompt. To do this we take the prompt we’ve created so far and surround it with the color tags, \e, like you do with HTML, you’ll need an end tag. Type:

PS1=”\e[1;32m [\u \w] $ \em”

If you mess up, hit your up arrow, fix your typos and try again.

I like a light green color which translates to 1;32m, note the semicolon between the numbers. The “m” ends the color. You should recognize the “[\u \w] $ “, then \em end color tag. You’ll now find all your text, including the prompt has changed color. An effect you might want to keep.

We add one little start bracket, “[” at the end between the e and m, to change the color in just the prompt, like so:

PS1=”\e[1;32m [\u \w] $ \e[m”

I know it looks crazy, but now just the prompt has color. What we wanted. On some systems \e will not work, if so, try \033 instead of \e as your color tag.

The colored prompt breaks the commands up nicely when your scanning back through history.

Now to the colors, the first number of the color, 1;32, is how light you want the color to be, a 0;32 is a darker green. If you prefer other colors here is the list:

Blue = 34
Green = 32
Light Green = 1;32
Cyan = 36
Red = 31
Purple = 35
Brown = 33
Yellow = 1;33
white = 1;37
Light Grey = 0;37
Black = 30
Dark Grey= 1;30

Not a huge color selection. You can get 256 colors. I’ll let you do the homework on that one.

For you adventuresome types, there’s much more you can do with color. You can color each element of the prompt differently.

PS1=”\[e[1;32m[\u] \[e[1;32m[\w] $ \e[m”

You can make your prompt one color and the command output another by altering the last end tag like so:

PS1=”\[e[1;32m][\u \w] $ \e[0;33m”

You now have green prompt, with yellow output. The only trouble with coloring the output, is you may not have your directories colored, which is something I like.

You can check this by typing 

ls -al –color

I picked this up after publishing this article.  If you want your command line to wrap around to a new line when it reaches the side of the terminal window, instead of staying on the same line and typing over the prompt, start the PS1 prompt, like so.

PS1=”\[\[e[1;32m][\u \w] $ \e[0;33m”

Notice the addition of “\[” at the start of the assignment.

How do we make what you’ve just been playing with permanent? We put the PS1 string that you now have carefully crafted in your .bashrc file. And then either close your shell and restart it, or type:

source .bashrc

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.