Practical example: Configuring your shell prompt

Why should I change my prompt?

Sooner or later, you will be confronted with badly managed or maintained systems. Your default prompt might be %, if your sysadmin provides his or her users with the default C shell prompt. The only thing that tells you, is that you are logged in. This way, you will end up typing pwd, who am i (or whoami) and date a lot. Whenever someone or something interrupts you, most of the time.

You can change your prompt to give more information. The prompt can't do everything, but it can make life much easier for the user as well as the system administrator.

Playing around with the prompt is a popular Unix game. The next sections should get you started.

What and where

The prompt is a shell environment variable called prompt in the C shell, and PS1 in the other shells. So it can be set to anything you want using the set prompt=value command in C shell, and PS1=value in Korn shell and Bash. Systemwide prompts may be set in the shell rc-file (e.g /etc/cshrc or /etc/bashrc), you own customizations are added in the .rc shell configuration files (e.g. .bashrc) in your homedirectory. Use your favorite editor.

We will discuss some examples, but there's much more to it. You should really read your shell manpage if you haven't by now.

When changing your prompt, always check whether or not a prompt has already been defined. If you don't, this might have strange consequences for some applications (e.g. when including the history number for a command (see below) the apllication might include:

(23) contence of the file (24)

in stead of just the file.

The process of checking whether the prompt has been set is called "kludging". These are the entries in your .rc-file:

Some examples to configure your C shell

Setting the prompt to contain your loginname:


 % set prompt="tille % "
 tille % 

Note

Including the % and space character at the end is useful to mark the difference between the prompt and the commands

My standard C prompt:


[tille@blubber ~]$ echo $prompt
[%n@%m %c]$

This prompt displays the username, an @ sign, the hostname and the current working directory.

Including the history number for each command:


 % set prompt ="\! % "
 23 % 

A solution when your prompt becomes to long: this prompt setting displays a three-line prompt including a blank line, a line with hostinformation and current working directory and a line with the command history number and the % sign. It nicely separates commands on the screen:


set hostname=`uname -n`
alias setprompt 'set prompt="\\
${hostname}:${cwd}\\
\! % "'
alias cd 'chdir \!* && setprompt'
setprompt	# sets the actual prompt

Some people don't want to be faced with the prompt. If you only use a couple of particular commands, set this prompt. It will display your options without preventing the execution of commands that you usually don't use:


if ($?prompt) then
set prompt='\\

Type "BitchX" to start chatting,\\
Type "pine" to read mail,\\ 
Type "startx" to start graphical applications,\\
Type "logout" to log out.\\
WHAT DO YOU WANT, MASTER? '
endif

Note

The Turbo C shell offers plenty of extra features to the promt variable, read the manual.

Some examples configuring the bash-prompt:

This is my bash prompt:


# /etc/bashrc

# System wide functions and aliases
# Environment stuff goes in /etc/profile

# are we an interactive shell?
if [ "$PS1" ]; then
    if [ -x /usr/bin/tput ]; then
      if [ "x`tput kbs`" != "x" ]; then 
	# We can't do this with "dumb" terminal
        stty erase `tput kbs`
      elif [ -x /usr/bin/wc ]; then
        if [ "`tput kbs|wc -c `" -gt 0 ]; then 
	# We can't do this with "dumb" terminal
          stty erase `tput kbs`
        fi
      fi
    fi
    case $TERM in
	xterm*)
	    PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"'
	    ;;
	*)
	    ;;
    esac
    [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "
    
    if [ "x$SHLVL" != "x1" ]; then # We're not a login shell
        for i in /etc/profile.d/*.sh; do
	    if [ -x $i ]; then
	        . $i
	    fi
	done
    fi
fi