Custom Terminal Prompt

For those who do not know what a terminal prompt is (bash shell prompt), it’s the thing you see when you open up the terminal.

The default looks like “user@host: directory $” or in my case that would be “rw@kaper:~$”.

You can make it look like anything you want to. Take a look at mine:

The file we are going to edit is balled .bashrc.

Open it using your favorite editor.

gedit .bashrc

Look for this line in the document that has opened:

# set a fancy prompt (non-color, overwrite the one in /etc/profile)
PS1=”xxxxxx”

note: it will not actually say xxx

Everything that you will put in place of xxxxx will be displayed in your bash prompt.

Let’s say you set PS1=”hello,  please enter a command”, then “please enter a command” will appear in your terminal instead of “user@host:directory$”.

You can use special signs that will output something useful like the date or the time.

These are the ones you can use

  • \a : an ASCII bell character (07)
  • \d : the date in “Weekday Month Date” format (e.g., “Tue May 26”)
  • \D{format} : the format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required
  • \e : an ASCII escape character (033)
  • \h : the hostname up to the first ‘.’
  • \H : the hostname
  • \j : the number of jobs currently managed by the shell
  • \l : the basename of the shell’s terminal device name
  • \n : newline
  • \r : carriage return
  • \s : the name of the shell, the basename of $0 (the portion following the final slash)
  • \t : the current time in 24-hour HH:MM:SS format
  • \T : the current time in 12-hour HH:MM:SS format
  • \@ : the current time in 12-hour am/pm format
  • \A : the current time in 24-hour HH:MM format
  • \u : the username of the current user
  • \v : the version of bash (e.g., 2.00)
  • \V : the release of bash, version + patch level (e.g., 2.00.0)
  • \w : the current working directory, with $HOME abbreviated with a tilde
  • \W : the basename of the current working directory, with $HOME abbreviated with a tilde
  • \! : the history number of this command
  • \# : the command number of this command
  • \$ : if the effective UID is 0, a #, otherwise a $
  • \nnn : the character corresponding to the octal number nnn
  • \\ : a backslash
  • \[ : begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
  • \] : end a sequence of non-printing characters

Notice it’s a back slash instead of the forward slash we linux users are used to.

Don’t be confused by all those options. It’s actually pretty easy.

Let’s say you want your prompt to read “The time is “.

You would simply type “The time” and then you would use one of the specials characters.

I see in the list there are a few time formats available. Since we don’t use am/pm here, let’s use “\t”.

So the line would look like this:

PS1=”The time is \t ”

(I put a space after \t, so there is a space between your prompt and the command you are going to write in the terminal)

A prompt like this

PS1=”Hour: \t \n Date: \d”

Would look like this

Hour: 12:12:12
Date: Fri Sept 05

The “\n” as you know from the list will create a new line.

With the help from the list you should be able to create your own interesting prompt now.

Chances are you are going to want to change the colours also.

This is done by adding “\e[x;ym” before the word you want to have that colour. If you want to stop that colour, use “\e[m”

The x;y will be replaced by the actual code for the colour.

You’ll need a list of colour codes, don’t you? Here you go.

Color Code
Black 0;30
Blue 0;34
Green 0;32
Cyan 0;36
Red 0;31
Purple 0;35
Brown 0;33
Blue 0;34
Green 0;32
Cyan 0;36
Red 0;31
Purple 0;35
Brown 0;33

Change the “0” to “1” for the lighter variant of that colour.

Let’s use give an example of this.

PS1=’ \e[0;31mUser:\e[m \u ‘

This give put “User:” a red colour and the actual username (\u) the default colour.

That should do it, you’re all set to start messing with your bash prompt.

If you are interested, this is my line (see picture on top of this page):

PS1=’ \e[0;31mUser:\e[m \u \n \e[0;31mHost:\e[m \H \n \e[0;31mDate:\e[m \d \n \e[0;31mTime:\e[m \t \n \e[0;31mDirectory:\e[m \w \n How can I help you Master? ‘

Have fun!

    • pnoque
    • September 5th, 2008

    Forward slash = /
    Back slash = \

    • linuxowns
    • September 6th, 2008

    Haha, you’re right.

    Fixed.

    • inf
    • September 6th, 2008

    What is that theme you’re using on that screenshot?

    • linuxowns
    • September 7th, 2008

    The theme called ClearGlow.

    It’s available on gnomelook.org

  1. Very awesome article. I’m always looking for ways to make my desktop just a bit more interesting, and this is one of my favorite ways to do so. Thanks for the great info.

  2. great article, stumbled

    • Patrick
    • September 21st, 2008

    I seem to have a different .bashrc file in my Ubuntu 8.04.

    Looks like this:
    # set a fancy prompt (non-color, unless we know we “want” color)
    case “$TERM” in
    xterm-color) color_prompt=yes;;
    esac

    I can’t seem to get a custom prompt to work.

    • Chris
    • September 22nd, 2008

    To Patrick: It’s just a few lines under that. Search for “PS1” (no quotes) if you’re having trouble finding it. It should look something like this:

    if [ “$color_prompt” = yes ]; then
    PS1=XXX ‘
    else
    PS1=’YYY’
    fi

    I’m not quite sure why, but for some reason mine wont accept the first option no matter what I do (uncommenting the force_color_prompt option a few lines above does nothing). I just use the latter option (it allows for color prompts, don’t worry). Hope this helps.

  3. Hi there mates, its enormous article concerning educationand entirely
    defined, keep it up all the time.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: