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.

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.

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?

g33kadmin

I am a g33k, Linux blogger, developer, student and Tech Writer for Liquidweb.com/kb. My passion for all things tech drives my hunt for all the coolz. I often need a vacation after I get back from vacation....

Leave a Reply

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