Tuesday, February 10, 2004

Setting Custom Prompts

O'Reilly's UNIX Power Tools, 3rd Ed inspired me to change the default prompts on my FreeBSD systems. My user account uses bash, so I made the following entry in ~/.profile to ensure my prompt shows my username, system name, and present working directory when I log in. The single straight quotes ensure that $PWD is substituted every time I change directories. If I had used double straight quotes, then $PWD would be fixed at whatever my current directory was when the shell was started. Single straight quotes helps us use "dynamic substitution." Using the back ticks sets the value of the system name.

PS1='$USER@`hostname -s`:$PWD '

Here's the result:


Changing .profile affects prompts seen when logging in to the terminal and remotely via SSH. This entry in ~/.profile doesn't influence the prompt seen by terminals started within X, as they are not "interactive" shells. (I still haven't figured that one out.) To ensure terminals in X share the same prompt, I made the same entry in the .bashrc file.

For tcsh, the default shell for root on FreeBSD (csh and tcsh are the same binary), I made this entry in /root/.cshrc after the 'set mail' line:

set prompt = "%n@%m:%/# "

Tcsh doesn't support much dynamic substition, but a read of the tcsh man page showed several variables built into the shell provide what I need. %n is the username. %m is the hostname up to the first period. %/ is the present working directory. All together the result looks good:


Keep in mind these prompts start to take up valuable terminal space. You can eliminate the %n in the csh prompt as the # shows you are root. If you primarily deal with a single user account, or don't care to know your login name, similarly eliminate the $USER@ from the bash prompt.

I will review UNIX Power Tools when done reading it, but I can already recommend buying it.