11 - Customizing the Linux Prompt

How To Customize the Prompt

Most Linux systems when you open a terminal line have a default prompt in one color (usually gray) that tells you your user name, the name of the machine you're working on by default.

The prompt can show all types of information such as : time, date, load,number of users, etc...

The prompt is defined in the variable called "PS1", to see how you actual prompt code looks like do this command :

bash:> echo $PS1

- so this bunch of signs,lines and numbers are the code that will output the content of you prompt line .

How can i alter or change my Prompt in Linux ?

-well, remember out variables tutorial and all that stuff is time to use all that .

- so now let's alter the PS1 variable so we can change our prompt look;

-nice you see that we have used the "export" command to overwrite the value of PS1 variable.

Note -this change will be available until we close this session.

How can i alter or change my Prompt in Linux permanently

-remember our .bashrc file ? So he is the answer to our question.

Let's go and edit this file to out needs:

Look of the .bashrc file and open it with any text editor you have:

Look inside this file the line "title to user@host" and alter the content of the PS1 variable , put what ever you want.

Note-if you are new to Linux make sure you save the string you are about to alter, so you can go back to it anytime you want.

After you have altered and saved the altered .bashrc file do the source ~/.bashrc command so that the changed should be applied.

And there you go your prompt has a new look.

Prompt creating command options

  • [a] - an ASCII bell character (07)
  • [ d] - the date in "Weekday Month Date" format (e.g., "Tue May 26")
  • [ e] - an ASCII escape character (033)
  • [ h] - the hostname up
  • [ 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
  • [ u] - the username of the current user
  • [ v] - the version of bash (e.g., 2.00)
  • [ V] - the release of bash, version + patchlevel (e.g., 2.00.0)
  • [ w] - the current working directory
  • [ W] - the basename of the current working directory
  • [ !] - 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
  • Color Codes Options

  • Black - 0;30
  • Dark Gray - 1;30
  • Blue - 0;34
  • Light Blue - 1;34
  • Green - 0;32
  • Light Green- 1;32
  • Cyan - 0;36
  • Light Cyan - 1;36
  • Red - 0;31
  • Light Red - 1;31
  • Purple - 0;35
  • Light Purple - 1;35
  • Brown - 0;33
  • Yellow - 1;33
  • Light Gray - 0;37
  • White - 1;37
  • Examples of how we can alter the Linux Prompt

    1-Show just the name of the user and the host name:

      bash:>  export PS1="u@h:>"

    - this will show the following:

        username@hostname:>

    2-Show host name and the actual working directory:

      bash:>  export PS1="h@w:>"

    - this will show the folowing:

        hostname@actual working ditectory:>

    3-Show hostname and time :

      bash:>export PS1="h[t]:>"

    - this will show the following:

      hostname@[actual time]:>

    4-Show the prompt with a different color then grey:

    This can be difficult to understand but i will put it in a easy way for you guys:

    Creating a PS1 values with colors has a certain syntax and order:

  • e - indicates the begining of the color.
  • {color}m - the "color" represent the color code shown early in the tutorial followed by "m" letter.
  • e[m - indicates the end of the color.
  • Syntax

    bash:> PS1="e[ color code you want and what ever you want to be displayed(host,time,user,etc..) e[m You can add more option, but it will be displayed with the default color "

                    bash:> export PS1="e[0;32mh[t]:>e[m"