BSD DevCenter

oreilly.comSafari Books Online.Conferences.

We've expanded our LAMP news coverage and improved our search! Search for all things LAMP across O'Reilly!

Search
Search Tips

advertisement

Listen Print Subscribe to BSD Subscribe to Newsletters
FreeBSD Basics

Understanding Shell Prompts

09/20/2000

Occasionally, a user will write the FreeBSD questions mailing list asking how to customize his prompt. Inevitably, the responses will include:

  • It depends on your shell
  • Read the manpage for your shell

While this may seem a brush-off at first glance, it is actually useful advice as prompts are shell specific. Furthermore, if one was to answer with a secret recipe like this:

set prompt = "${e}[1m${USER}@`dirs`${e}[0m% "

the prompt might work, but the original user would have no understanding of "why" this cryptic string of seemingly random characters affected the look of his prompt.

In today's article, we'll look at customizing the prompt for some of the more popular shells. By the end of this article, you should be able to "decode" the above recipe, and create your own custom-combo for your favorite shell.

The following table shows the default prompts for four different shells:

Shell

Executable

Default Prompt

Bourne

sh

$

C

csh

%

tcsh

tcsh

%

Bash

bash

$

Unless you specify otherwise, any user you create will be placed in the Bourne shell. In FreeBSD versions 4.0 and earlier, the root account receives the C shell; this has been changed to the tcsh shell for FreeBSD 4.1. Bash is not installed by default, but is available in the Shells section of the ports collection. Also note that the root user's prompt will be a #, regardless of the shell.

Before we can customize the prompt, we need to know what a shell variable is. Every Unix shell is really a programming language; programming languages use variables to store information. Variables allow you to change or vary their value, hence their name. Your prompt is an example of a shell variable; if you could not change its value, you would be stuck with the defaults of $, %, or #.

Variables can be stored in a file that is read by the shell when you log in or change shells. The following table shows which configuration file(s) can be read by each shell:

Shell Name

Configuration File(s)

Bourne

.profile

C

.cshrc, .login, .logout

tcsh

.tcshrc, .cshrc, .login, .logout

Bash

.bash-profile, .bash_login, .profile, .bashrc, .bashlogout

Since we'll be mucking about with shell variables, let's create a test user account to try out our changes. Become the root user and type:

/stand/sysinstall

and select Configure, User Management, and then User. Input a login ID for the test account and a password you'll remember, and change the Login shell to /bin/csh. When you're finished, leave /stand/sysinstall, exit out of the root account and log in as your test user.

Since you changed the user's shell to the C shell, your prompt should look like this:

%

The user will be in their home directory; to see the configuration files, type:

ls -a

Note that the -a is required, as files that begin with a "." are hidden files. Now type:

more .cshrc

to view the default settings for the C shell. Let's take a closer look at some of these entries:

alias h		history 25
alias j		jobs -l
alias la	ls -a
alias lf	ls -FA
alias ll	ls -lA

After the remarked-out comments section is a set of pre-defined aliases. Aliases are simply abbreviations for commands. Notice the alias called la: it is an abbreviation of ls -a. This means you could have typed la instead of ls -a to see your hidden files. Also note that you can type the letter h to receive your last 25 history entries; this sure beats typing the word history.

You can add your own aliases; they can be useful if you always want to use certain switches with a command. Some good aliases for beginners are:

alias ri	rm -i	#ask before deleting
alias mi	mv -i	#ask before moving over an existing file

Note that when you create an alias, you'll want to choose a name that is not already used by another command, like ri or mi. And be careful with aliases; if you get used to being prompted because of an alias, you may forget this is not the default behavior when you are at another computer or logged in as a different user.

Pages: 1, 2, 3

Next Pagearrow




Sponsored Resources

  • Inside Lightroom
Advertisement

Sponsored by:

O'Reilly Media

©2009, O'Reilly Media, Inc.
(707) 827-7000 / (800) 998-9938
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
About O'Reilly
Academic Solutions
Authors
Contacts
Customer Service
Jobs
Newsletters
O'Reilly Labs
Press Room
Privacy Policy
RSS Feeds
Terms of Service
User Groups
Writing for O'Reilly
Content Archive
Business Technology
Computer Technology
Google
Microsoft
Mobile
Network
Operating System
Digital Photography
Programming
Software
Web
Web Design
More O'Reilly Sites
O'Reilly Radar
Ignite
Tools of Change for Publishing
Digital Media
Inside iPhone
O'Reilly FYI
makezine.com
craftzine.com
hackszine.com
perl.com
xml.com

Partner Sites
InsideRIA
java.net
O'Reilly Insights on Forbes.com