Monday, August 19, 2013

How to change the command prompt 'host & user color

[ To change the color of  'hostname' in command prompt ]

You can change the color of your shell prompt. To make your own life quite easy while working at the command prompt. Your current prompt setting is stored in a shell variable called PS1. There are other variables too, like PS2, PS3 and PS4.


Bash displays the primary prompt PS1 when it is ready to read a command, and the secondary prompt PS2 when it needs more input to complete a command. Bash allows these prompt strings to be customized by inserting a number of backslash-escaped special characters.


* Task: Display current BASH prompt (PS1)

# $ echo $PS1

Sample outputs:

[\\u@\h \\W]\\$

    or

 \[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$
  By default the command prompt is set to [\u@\h \W]\$. The backslash-escaped special characters are decoded as follows:

    \u: Display the current username .
    \h: Display the hostname
    \W: Print the base of current working directory.
    \$: Display # (indicates root user) if the effective UID is 0, otherwise display a $.

* Task: Add colors to the prompt.

To add colors to the shell prompt use the following export command syntax: '\e[x;ym $PS1 \e[m'
Where,

    \e[ : Start color scheme.
    x;y : Color pair to use (x;y)
    $PS1 : Your shell prompt variable.
    \e[m : Stop color scheme.

* To set a red color prompt, type the following command

$ export PS1="\e[0;31m[\u@\h \W]\$ \e[m "
A list of color codes
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

Note: You need to replace digit 0 with 1 to get light color version.

*Task: How do I make the prompt setting permanent?

 Your new shell prompt setting set by $PS1 is temporary i.e. when you logout setting will be lost. To have it set every time you login to your workstation add above export command to your $HOME/.bash_profile file or $HOME/.bashrc file.

$ cd
$ vim .bash_profile


        or 

$ vim $HOME/.bashrc    

add this line in the end of the this file.

export PS1="\e[0;31m[\u@\h \W]\$ \e[m"

(save and quit from the file )

This will permanent color change

---------------------------------------------------------------------------------

For RHEL/Centos

 go to the file

#vim  .bashrc

 and edit in end of the page this line:-

export PS1="\e[0;34m[\u\[\033[0;36m\]@\[\033[0;31m\]\h\[\033[0;33m\] \w]# \e[m"

 [ and save and quit the file ]

------------------------------------------------

For Ubuntu

*************
  go to the file

$vim  .bashrc

#force_color_prompt=yes           [ remove the '#' singh from the line ]

use this line :-

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

[ after this save and quit the file ]

--------------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment