Tuesday 30 October 2012

Customize the CMD prompt

When you login the linux system for the first time, the following prompt you will see.

[root@localhost ~]#

This means you are login with root user with hostname localhost and you are in the root directory.

If you are logged in with a simple user's account then

[joy@localhost ~] $


What actually this means, We can also represent the above command line with below:

[\u@\h\W]\$

Change options for the prompt:

\d : the date Weekday Month Date format
\h : the hostname up to the first ‘.’
\A : the current time in 24-hour HH:MM format
\u : the username of the current user
\w : the current working directory, with $HOME abbreviated with a tilde
\$ : if the effective UID is 0, a #, otherwise a $


Also if we want to change this console that we see by default, we can change it as well. There is a variable called PS1, we need to edit that variable in order to customize the look of the prompt. To see the default value of the variable enter the below command:

[root@host1 ~] # echo $PS1
[\u@\h\W]\$

This is by default value, we need to customized values to this variable and our task is completed. Here are below some of the example:

[root@host1 ~] # export PS1='\e[0;36m[\u@\h \W]\$ \e[m '
[root: myself_sahil~]#

0;36m is for color specifications..

There are a few variable also that helps to customize the prompt. These are as below:

\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 base name of the shell’s terminal device name
\n : newline
\r : carriage return
\s : the name of the shell, the base name 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 base name 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



Create a colored prompt:

You may want to create a color prompt that you can use for visibility. In this example the hostname has been dropped to make a shorter prompt and the prompt is turned red but the commands that you enter will be black. The export command will change these features.

sahil@ub:~$ export PS1=’\e[0;31m[\u:\w]\$ \e[m ‘
[sahil:~]$

This will color the prompt but not any commands that you enter.

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

Replace digit 0 with 1 for a lighter color.

Make Changes permanent:

All of the changes you make will be lost when you close the terminal or log out. Here are directions to make them permanent.

Ubuntu 

The .bashrc file in each user’s home directory allows you to change the default for the prompt to a color prompt by uncommenting the line:

#force_colored_prompt=yes

Unfortunately a typo in the line must also be corrected so that it should read:
force_color_prompt=yes

Ubuntu or CentOS
Place your custom prompt in the user .bashrc file with this command:

export PS1=’\e[0;31m[\u:\w]\$ \e[m ‘

No comments:

Post a Comment