Customize your unix shell prompts

You can customize your unix shell prompt in several ways, including setting colors, and setting the title of your Xterm sessions.

Shell prompt string variables

Bash prompts are defined by the environment variables PS1 and PS2. The former is the normal prompt; the latter is used when the shell needs further command line input. See “man bash”.

Tcsh uses variables “prompt” and “prompt2” in the same way. See “man tcsh”.

Before working with the prompt variable, you should check that it exists. Otherwise, you will get an error when you do a non-interactive login (running a script, for example).

Some useful strings to put in the title and the prompt be had by expanding the shell strings, in Bash, they are

\u user login name
\h machine name
\w current working directory: complete path
\W current working directory using “~” for $HOME

Xterm window titles

It is customary to change the title of an Xterm window at the same time as setting the prompt. A distinct title for each terminal window can be very useful if you have a lot open.

The Xterm window title is altered by emitting a special Xterm escape sequence: It is initiated by “\e]0” (escape right-bracket 0) and terminated by “\a” (the bell).

ANSI color codes

The color of the prompt is set by expanding the escape sequence “\e[sm”, where s is a semicolon-delimited list of ANSI color codes: “\e[31;44;1m” would set the foreground color to red, the background to blue, and the font in bold face; (The “\e” is the ASCII Escape character. Don’t forget to terminate the sequence with the “m” character.)

Binary sequences in the environment variables need to be set off by indicators that they have zero width, or else the shell won’t calculate correctly the width of the prompt. Bash encases such things with slash-brackets “\[ .. \]”, whereas Tcsh uses percent-brace “%{ .. %}”.

The codes:

0 restore default color
1 brighter
2 dimmer
4 underlined text
5 flashing text
7 reverse video
black red green yellow blue purple cyan white
foreground 30 31 32 33 34 35 36 37
background 40 41 42 43 44 45 46 47

Note: the bold characters may or may not be available. In Unicode enabled terminals, support has gone away. Also, your Xterm may have bold turned off by command line flags.

ANSI color table script

Here is a unix shell script that should display all the ANSI colors in your terminal.

colors.sh

Download it and run it in a shell.

Bash example

(put in .bashrc):

START_COLOR="\[\e[32;1m\]"
END_COLOR="\[\e[0m\]"
if [ "#TERM" ]; then
	case $TERM in
	xterm*)
		PS1="$START_COLOR\h$END_COLOR \W$START_COLOR]$END_COLOR "
		PS2="$START_COLOR]>$END_COLOR "
		TITLEBAR='\[\033]0;\u@\h:\w\007\]'
		PS1="${TITLEBAR}${PS1}"
		;;
	linux*)
		PS1="[$START_COLOR\h\[$END_COLOR \w] "
		PS2="$START_COLOR>\[$END_COLOR] "
		;;
	vt100*)
		PS1="[$START_COLOR\h\[$END_COLOR \w] "
		PS2="$START_COLOR>\[$END_COLOR] "
		;;
	*)
		PS1="bash\\$ "
		;;
	esac
fi

Tcsh example

(put in .login or .tcshrc):

        # The \e[...m sequences set colors
	# The %{%} brackets delimit zero-width commands
	# in tcsh, the host is %m, user is %n, etc.  See man tcsh.
setenv TERM_TITLE "%{\e]0;%n@%m\a%}"
setenv START_COLOR "%{\e[32;1m%}"
setenv END_COLOR "%{\e[0m%}"
if ( $?prompt ) then
        switch ($TERM )
        case "xterm*":
                set prompt="${TERM_TITLE}${START_COLOR}%m~]${END_COLOR} "
                set prompt2="${START_COLOR}>${END_COLOR} "
                breaksw
        case "linux*":
        case "vt100*":
                set prompt="${START_COLOR}%m~]${END_COLOR} "
                set prompt2="${START_COLOR}>${END_COLOR} "
                breaksw
        default:
                set prompt="%n:%~] "
                set prompt2=">% "
                breaksw
        endsw
endif