Configuring Your OS X Unix Environment
- Finding Configuration Files
- Configuring Your Shell
- Environment Variables
- Changing your PATH
- Shell Aliases
- Shell Settings
- Configuring vi
Many user-configurable Unix programs (such as your shell) read configuration files when they start up. These configuration files contain settings and commands that determine how the programs will behavefor instance, the files can modify the list of places your shell looks for the commands you enter (that list is called your PATH).
You change your Unix environment to have it more closely match your personal preferences and to shape it more closely to the way you work.
Examples of configuring your Unix environment include:
Customizing your shell prompt so that it displays information you want to see.
Creating shortcuts for commonly used command/option combinations (these can be "aliases," but they are distinct from the aliases you create in the Finder, and from the shell functions, which are short scripts that are part of your private configuration).
Making it easier to use additional software you install; for example, if you add /Developer/Tools to your PATH, then you can use the commands in the /Developer/Tools directory without typing their full pathnames.
Configuring specific programs such as vi to turn on various options whenever you use them, much the same way that traditional Mac programs often have a preferences dialogue box.
The first program to configure is your shell, since your shell is the primary program you use to interact with Unix. We will also show you how to configure the vi editor by editing a configuration file it uses (see Chapter 6, "Editing and Printing Files," to learn how to use vi).
It should come as no surprise by now that you configure your shell by editing text files.
Finding Configuration Files
User-configurable Unix programs (including your shell, the vi editor, and others) look for configuration files in your home directory when they start up. Most of the commands you have learned so far are not user-config-urable; neither the ls nor the cd command uses configuration files, for example. They do accept options on the command line but do not read any configuration files when you run them.
Many configuration filenames begin with a dot (.), so they are called dot files (use ls -a to see them). Often the filenames end in "rc" (for resource). For example, the main config-uration file for the tcsh shell is ~/.tcshrc, a configuration file for the bash shell is ~/.bash_profile, and the configuration file for the vi editor is called ~/.exrc (ex is an older editor, and vi provides a "visual interface" for it). There are actually several config-uration files available for each shell, and Table 7.1 lists the more common ones. In this chapter, we will concentrate on the ones you would change in the course of normal use. These files each have settings and commands for the particular program being configured. (Remember that ~ [the tilde] is a synonym for your home directory.)
Configuration files for shells are actually scripts. This means they are a series of commands written in the scripting language for the corresponding shell. They make use of variables, if-then conditions, and other scripting elements such as loops. (See Chapter 9, "Creating and Using Scripts," for more on scripts.)
Table 7.1 A Summary of Common Shells
Shell |
Name and Description |
sh |
Bourne shell. The oldest and most standardized shell. Widely used for system startup files (scripts run during system startup). Installed in Mac OS X. |
bash |
Bash (Bourne Again SHell) is an improved version of sh. Combines features from csh, sh, and ksh. Very widely used, especially on Linux systems. See the Bash Reference Manual online (http://www.gnu.org/manual/bash/). Installed in Mac OS X. |
csh |
C shell. Provides scripting features that have a syntax similar to that of the C programming language (originally written by Bill Joy). Installed in Mac OS X. |
ksh |
Korn shell. Developed at AT&T by David Korn in the early 1980s. Ksh is widely used for programming. It is now open-source software, although you must agree to AT&T's license to install it. See the KornShell Web site (http://www.kornshell.com). |
tcsh |
An improved version of csh. The t in tcsh comes from the TENEX and TOPS-20 operating systems, which provided a command-completion feature that the creator (Ken Greer) of tcsh included in his new shell. Wilfredo Sanchez, formerly lead engineer on Mac OS X for Apple, worked on tcsh in the early 1990s at the Massachusetts Institute of Technology. |
zsh |
Z shell. Created in 1990, zsh combines features from tcsh, bash, and ksh, and adds many of its own. Installed in Mac OS X. The Web site for Z shell is http://zsh.sourceforge.net. |
Am I Configuring the Terminal Application or My Shell?
There's an important distinction to understand here.
The Terminal application you are using to access the command line in Mac OS X is not the same as your shell.
Terminal is a regular Mac OS X graphical application, like your Web browser or word processor. When you open a new window in Terminal, the application runs the appropriate Unix shell (determined by the Terminal application's preferences). Terminal is the program that is handling the screen display and keyboard input for the shell. When you type something in Terminal, the Terminal application passes that to the shell, and when the shell produces output, Terminal draws it on your screen.
The subtle point here is that there are actually other ways besides Terminal in which you can use your shell. One example: You can connect to your Mac using the command line over a network from another machine, which we'll cover in Chapter 10, "Interacting with Other Unix Machines." So when we tell you in this chapter that a change you make will take effect "in the next Terminal window you open," that is really a shorthand way of saying that the change will take effect in the next instance of your shell that you run, and that the easiest way to see it is to open a new Terminal window.