Discussion of this wiki can be found here

Customizing Bash Prompt

If you have difficulties using the terminal because the prompt isn't visible enough or you would simply like to customize its appearance, this howto is for you. It should work on any distribution utilizing the bash shell.

Fire up your terminal and stay in your home directory.


First back up your default .bashrc file, so you can always fall back on it should you mess something up.

cp .bashrc .bashrc-backup

The .bashrc file defines many aspects of the shell environment, including showing the prompt.

Preparing .bashrc

  • Use your favorite text editor to edit the original .bashrc file, eg.:

gedit .bashrc

Locate this if/then statement:

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '

Bash reads PS1 variable to define the primary prompt and PS2 for a secondary prompt (used when writing multi-line commands). In this "if" block, the first PS1 is the prompt that will be shown when color is turned on, and the second one (after "else") is used when no color is desired. As you can see, color codes are quite tiring to use. Imagine having to write things like \[\033[01;32m\] all over again- it's quite tiresome. Fortunately, bash lets us define variables for the different colors. Paste this code above that "if" block:

# ANSI color codes
RS="\[\033[0m\]"    # reset
HC="\[\033[1m\]"    # hicolor
UL="\[\033[4m\]"    # underline
INV="\[\033[7m\]"   # inverse background and foreground
FBLK="\[\033[30m\]" # foreground black
FRED="\[\033[31m\]" # foreground red
FGRN="\[\033[32m\]" # foreground green
FYEL="\[\033[33m\]" # foreground yellow
FBLE="\[\033[34m\]" # foreground blue
FMAG="\[\033[35m\]" # foreground magenta
FCYN="\[\033[36m\]" # foreground cyan
FWHT="\[\033[37m\]" # foreground white
BBLK="\[\033[40m\]" # background black
BRED="\[\033[41m\]" # background red
BGRN="\[\033[42m\]" # background green
BYEL="\[\033[43m\]" # background yellow
BBLE="\[\033[44m\]" # background blue
BMAG="\[\033[45m\]" # background magenta
BCYN="\[\033[46m\]" # background cyan
BWHT="\[\033[47m\]" # background white

Now you don't have to write all that ANSI code over and over again. For instance, you can just use $FBLE to set foreground (text) to blue, instead of \[\033[34m\].

Setting Prompt Without Color

Now that you have this prepared, you can design your own prompt. Start with the one without color for simplicity. The line for this prompt is located just below the "else" line of the previously mentioned statement. Comment out the default PS1 that's already there by typing a hash mark ( # ) at the beginning of the line, then define your own PS1 and PS2 just below it. This way you still have the original prompt to fall back on or reference for syntax.

It should look like this:

if [ "$color_prompt" = yes ]; then
\[\033[00m\]\$ '
    #PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
    PS1="[ ${debian_chroot:+($debian_chroot)}\u: \w ]\\$ "
    PS2="> "

Secondary prompt is simple. It shows just >. The primary prompt is more complicated. It contains special characters and sequences for additional info about a session. You can find a list of those special characters in bash's man page under section PROMPTING:

man bash

The example above uses \u (username), \w (current working directory) and, of course, \\$ (the $ or # symbol, depending on your privileges). The "${debian_chroot:+($debian_chroot)}" prints the name of the current chroot if applicable. It's wise to leave it a part of the prompt and located and the beginning. Configured this way the primary prompt will appear thus (when in the home directory):

[ penguin: ~ ]$

You can also add whatever text you wish to the prompt. If I wanted my prompt to say "$USER is awesome" (just for example), I would write my prompt as

PS1="[ ${debian_chroot:+($debian_chroot)}\u is awesome: \w ]\\$ "

The prompt will now read

[ penguin is awesome: ~ ]$

The tilde ( ~ ) will change to the path depending upon the location in the fileystem.

Setting Prompt With Color

If you wish to use a colored prompt, locate this line in .bashrc


and remove the # from it, which will cause bash to default to a colored prompt. Comment out the stock PS1 and write your own on beneath it. Or, if you already have a non-colored PS1 and PS2 just copy-paste them from below "else" (colorless versions). You now have the base layout and you just need to add some color to it.

The simplest way to add color is to instert a variable from the list of color aliases given above. First instert $RS at the end of both PS1 and PS2 so that all other text in the terminal retains its formatting. All ANSI color codes change only the text behind them, and you can only turn off $HC, $UL and $INV codes by using $RS. Sample prompt:

PS1="$HC$FYEL[ $FBLE${debian_chroot:+($debian_chroot)}\u$FYEL: $FBLE\w $FYEL]\\$ $RS"

This changes my prompt to look like this:


To test the new prompt, save the .bashrc and reopen your terminal emulator. All changes will take effect. To see the secondary prompt, enter


and press enter.

Tip: Most terminal emulators also allow you to change terminal text color if you want to explore colorizing more. It's frequently found in "Preferences".

Reverting to old settings

  • If for some reason you wish to return the old prompt or if you broke your .bashrc, the easiest way is to just recover old .bashrc file you backed up. Run a terminal and use:

cp .bashrc-backup .bashrc

This will restore the default .bashrc.

Originally posted The Ubuntu Forums ubuntuforums.org

CustomizingBashPrompt (last edited 2012-07-18 11:04:06 by elfy)