The first thing I always change in my .bashrc and .profile files on any Unix is a bash prompt.
Bash prompt on Unix is defined by environment variable PS1. Default prompt on most systems is usually set to somethings stupid. I need two things from my prompt: host name and current directory name. I also like '>' sign to be at the end of a prompt, not '$' or '#'. So I put into my .bashrc:
PS1='\h:\w>'
I also like my prmpt to be colored so I could visually distinguish it from the surrounding text - it really helps when you have output from several consecutive commands in your terminal. There are many articles on the Web which advise how to add colours to the prompt, and it seemingly works but it is easy to do it wrong.
Each time I am asked: 'If I need to wrap long command in bash, my command line is messed up, why?', I ask back: 'Do you have a coloured prompt?'. I invariably hear 'Yes'.
The problem is that terminal accounts colour-changing control symbols for string length, but in fact they do not take additional space. That discrepancy messes up a multiline input.
So here's the right way to enter colour changing control symbols:
\[033[B;Cm\]
Instead of B you must put either 0 - for normal colours, or 1 - for bright colours. Substitute C with a number of a colour:
- 30 - Black/Dark grey
- 31 - Red
- 32 - Green
- 33 - Yellow
- 34 - Blue
- 35 - Magenta
- 36 - Fuscia
- 37 - White/light grey
- 38 - Default foreground color. Use it at the end of the prompt, so that further text is not coloured.
I like to have bright yellow prompt (I use black background), so I put into my .bashrc:
PS1="\[\033[1;33m\]\h:\w>\[\033[0;38m\]"
export PS1
If you want other information to appear in bash prompt, here is the reference:
\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 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
\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 direc tory
\! 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 con trol sequence into the prompt
\] end a sequence of non-printing characters
So, jump into your favourite editor, hack your .bashrc. Experiment, and I am sure many of you will find coloured prompt handy.
There are many other ways you may customize your bash prompt. It is actually possible to include output of any programs into the prompt or even change the prompt (and colours within it) depending on some external conditions. For example you might turn your prompt red if last program finished with non-zero error status (which means something bad or unexpected happened) or green if everything went well. But this is probably a topic for a separate article.
Posted in Software frolov's blog | add new comment
Submitted by frolov on Wed, 2005-09-21 22:11.



