BSD DevCenter
oreilly.comSafari Books Online.Conferences.


Understanding Shell Prompts
Pages: 1, 2, 3

Let's move down to the shell variables:

if ($?prompt) then
	# An interactive shell -- set some stuff up
	set history = 100
	set savehist = 100
	set mail = (/var/mail/$USER)

Note the syntax: set variable = [value]

Value is in brackets -- [] -- as it is optional. This syntax is specific to the C and tcsh shells.

If you wish to change your prompt, the name of the C or tcsh shell variable is prompt. You can set as many values as you want for the prompt variable, as long as they are enclosed within quotation marks. The actual values differ slightly for the C shells and the tcsh shells; if you are using FreeBSD 4.0 or earlier, follow the instructions for the C shell. If you are using FreeBSD 4.1, follow the instructions for the tcsh shell.

Let's try a very simple prompt that works in both the C and tcsh shells. Using your favorite text editor, open up the .cshrc file in your test user's home directory. Add the following line at the very end of the file, just before the endif line.

set prompt = "${USER}% "

Save the file and log out. When you log back in as the test user, your prompt should show your user name, followed by a % followed by a space, and then your cursor. Let's pick apart the string of characters we assigned to the prompt shell variable.

First off, we enclosed the entire value in quotation marks. It is good practice to always do this; it is mandatory if you have any spaces anywhere in your string of characters.

The ${USER} is actually an environment variable. While shell variables are specific to a shell, environment variables are read by all programs (including shells). You can recognize variables as they always start with a $ and are usually enclosed in curly braces -- {}. By convention, environment variables are named in upper case characters, while shell variables are in lower case. If you wish to see your other environment variables, type the following at your command prompt:


If you wish to see your shell variables, type:


The ${USER} environment variable contains your login name. We can tell the shell prompt variable to read this value when it sets your prompt.

Finally, we put a % after the login name to remind ourselves we are still in the C shell. I like a space between the % and the cursor, so I included one before the last quotation mark.

This prompt is useful as you will always be able to tell at a glance who you are logged in as at a virtual terminal.

I'm a very forgetful person and was forever using the pwd command to figure out where I was in the directory structure. Fortunately, this information can be included in the prompt. To do this in the C shell, replace your set prompt line with the following text:

alias cd 'chdir \!* && set prompt="`dirs`% "'

If you've typed the above without any typos, when you log off and back on, you'll get the regular % prompt. However, after you type cd, your prompt will always tell you what directory you are in, with your home directory shortened to the ~ symbol.

We actually had to do a bit of C programming to get this prompt to work. The C shell understands the ${cwd} or current working directory variable; unfortunately, this will only show what your current directory was when you logged in. We had to create an alias to the cd command if we wanted our prompt to continue to change as we changed directories.

We also had to use the backquote, or ` -- this is on the same key as the ~ symbol. Don't confuse it with this quote -- ', which is on the same key as ". Back quotes are used for command substitution. We used it around the dirs command that tells the C shell to print out the name of the current directory. If you forget the ` quotes, your prompt will literally display the word dirs, instead of the result of the dirs command.

The other new characters we used were &&. This tells your shell that "I want you to do one thing, and when that successfully finishes, I want you to do this next". In summary, we told the shell that when we use the cd command, not only did we want it to change our directory, we also wanted it to set our prompt to show the name of the directory we changed to.

Pages: 1, 2, 3

Next Pagearrow

Sponsored by: