BSD DevCenter
oreilly.comSafari Books Online.Conferences.


Understanding Shell Prompts
Pages: 1, 2, 3

It is much easier to accomplish this same prompt in the tcsh shell, as it has a built-in variable for this purpose. The following string works for the tcsh shell:

set prompt = "%~ % "

The %~ is the built-in variable that shows the current working directory, with a ~ to represent the user's home directory. Again, I used the % to remind myself I was in the C shell (tcsh is really the C shell with extensions to its functionality), and I kept a space between my prompt and the cursor.

I usually like to remember both who I am and where I am, so I set my C shell prompt like this:

alias cd 'chdir \!* && set prompt="${USER}@`dirs`% "'

and my tcsh shell prompt like this:

set prompt = "${USER}@%~ % "

Notice that I've just joined all the desired commands together and enclosed them in quotation marks. I also put an @ as my separator between the username and the current working directory, so my prompt looks like this:

genisis@~ %

We've input some useful information in our prompt, but it would be nice to have its text stand out so we don't confuse it with the other text on the screen. To bold your prompt in the C shell, replace your set prompt text with these lines:

set e="`echo x | tr x \\033`"
alias cd 'chdir \!* && set prompt="${e}[1m${USER}@`dirs`${e}[0m% "'

Since the C shell does not have a built-in variable for bolding text, we had to create our own variable which we called e. Because this is a shell variable, we gave it a name in lowercase; we then used our homemade variable when we set our prompt whenever we did this:


When we created e, we referred to the ASCII character 033; this character represents the escape character. Every time we referenced ${e}, the shell did the equivalent of pressing the ESC key. Most terminals will interpret ESC[1m to mean "start bolding text." When you want to stop bolding text, ESC[0m tells the terminal to turn off that attribute.

If you prefer, you can underline text by replacing the [1m with [4m. Or you can cause your text to flash by using [5m instead. Don't forget to use [0m to turn off the attribute, or your whole screen will flash, which makes it very hard to work with text!

I usually change root's prompt to be bold and flashing; this can be quite irritating, which reminds me to be root only when I absolutely have to be. To do this, become root and add the following lines to /root/.cshrc. (Note: The line beginning with alias is broken into a second line for the sake of formatting here; it should be entered as one line in your file):

set e="`echo x | tr x \\033`"
alias cd 'chdir \!* && set 
  prompt="${e}[1m${e}[5m${USER}@`dirs`${e}[0m% "'

Note that you can stack the [1m and [5m commands together; just remember to call ${e} before each one so it will press the ESC key for you.

Again, the tcsh shell has built-in variables to accomplish bolding and underlining, so you don't have to define your own variable first. To bold text in the tcsh shell, use this line to set your prompt:

set prompt = "%B${USER}@%~%b% "

In this command, %B starts bolding text and %b stops bolding. To underline text, use %U to start and %u to finish. The tcsh shell has many built-in variables that will let you put the date, time, your hostname, and colors into your prompt.

We've only touched the surface of the possibilities of setting your prompt. You'll want to read the man page for your favorite shell to see what else you can set in that shell's prompt. For example, the C shell's executable is csh, so type:

man csh

to read the man page for the C shell.

The first thing you'll notice about a shell's man page is that it is very long and contains a lot of advanced programming stuff you've probably never heard of. Fortunately, man pages have a search utility; the trick is to try to think of a word that is unique enough to zero in on what you're looking for. To do a search while in a man page, type:


For example, you could try:

/shell variables

which will bring your cursor to the next line that contains the words "shell variables". If you still aren't where you want to be, type / and press Enter to go to the next occurrence of your search string.

If you have a favorite shell prompt you would like to share, or an URL that contains some useful shell prompts, post them to the BSD Support Forum on Daemon News and we'll compile a list of readers favorites.

A better search would be something more unique like:


This would bring you to the %B built-in variable if you were in the tcsh man page. Even if this wasn't the variable you were looking for, you would still be in the shell variable section of the man page.

If you're ever in a long man page and happened to make note of a page number, /60 would take you back to page 60.

Once you find an interesting shell variable or value, try it out in a test user account first to see if you like the results. You don't have to log out and back in to test every change. For example:

source .cshrc

will force the C shell to re-read the .cshrc file and apply any changes you've made.

Next week, we'll discuss running jobs with the cron daemon.

Dru Lavigne is a network and systems administrator, IT instructor, author and international speaker. She has over a decade of experience administering and teaching Netware, Microsoft, Cisco, Checkpoint, SCO, Solaris, Linux, and BSD systems. A prolific author, she pens the popular FreeBSD Basics column for O'Reilly and is author of BSD Hacks and The Best of FreeBSD Basics.

Read more FreeBSD Basics columns.

Return to the BSD DevCenter.


Sponsored by: