Friday, August 8, 2014

Change the prompt color in BASH

The default prompt for RaspberryPi is little hard to read with PuTTY. The color of path name is deep blue and I can barely read it when the background color is black. I can probably change the background color with PuTTY settings but you can also change the color of prompt itself.

The prompt setup was once one of the most important features of shell programs long time ago and now people barely remember that we can change the colors. lol

The way you can change the color varies depending on what shell program you are using. You can check which shell program you are using by opening a file, /etc/passwd. The line is separated by colons, :. The first column shows the ID and the last column shows which shell program the ID is using. If other shell programs are install, you can change it with a command, "chsh", but bash (Bourne-Again SHell) is good enough so I am not gonna bother.

You can see what's the current prompt setting is by typing "echo $PS1" or "echo $PS2". PS2 is for the case when the command line is multiple lines, which you will barely see.

The default prompt setting of RaspberryPi is little complicated. This setup is stored in ~/.bashrc and if you modify the file, your change will become permanent; in other words, unless you modify the file, ~/.bashrc, your prompt will go back to where it was whenever you login again.

Let's start with a simple example: "export PS1="\u@\h \t \W\$ ".

The human unreadable cryptic code is explained in the manual page of bash: "man bash"
  • "\u" means the user name of the current user
  • "\h" means the hostname up to the first '.'
  • "\t" means the current time in 24-hour HH:MM:SS format
  • "\W" means the basename of the current working directory, with $HOME abbreviated with a tilde.
I am very tempted to use \! as well but it will make the prompt too long.

Now it does what I wanted; there is no blue colored characters that I cannot read. But I am also losing the fancy green colored id and hostname. So the coloring is gone.

Coloring is little more tricky. The syntax is like this:
  1. "EscapeCharacter + [ + color number + m" : this will change the color and the color will be applied to later characters.
  2. "EscapeCharacter + [ + m" : this will bring the color back to normal.
  3. There are two ways to type-in "EscapeCharacter": \033 or \e.
  4. More than one color numbers can be listed with separation mark, ";".
  5. Color numbers for characters are from 30 to 37: black(30), red(31), green(32), yellow(33), blue(34), magenta(35), cyan(36), white(37).
  6. Background colors are from 40 to 47.
  7. Color number 01 will make the color brighter.
 So what I want is bright green on the user name and hostname so the command is: export PS1="\e[01;32m\u@\h\e[m \t \W\$ "

I want to apply it every time I login. In order to change the prompt permanently I need to change the file, ~/.bashrc. The screenshot above shows that I commented out the original prompt setting with a mark "#" and I added a new prompt setting.

Now when I login, the prompt is changed. Also the current path name is readable. Well... It says it is time to sleep. lol

*Note that I don't type-in password nor ID. lol. You can do it with one of my previous articles.


  1. There are more tricks with ANSI escape character such as remembering and changing the position of the cursor. When I was kid, I had 4DOS with a fancy prompt. The prompt always showed current time on the top of right side and the memory usage on the top of left side with fancier colors. I loved it. lol

  2. It seems that the color changing code needs to be enclosed with \[ and \] as described here:


About Me

My photo
Tomorrow may not come, so I want to do my best now.