O'Reilly logo

Learning Unix for Mac OS X, Second Edition by Dave Taylor, Brian Jepson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Customizing Your Shell Environment

The Unix shell reads a number of configuration files when it starts up. These configuration files are really shell programs , so they are extraordinarily powerful. Shell programming is beyond the scope of this book. For more detail, see Paul DuBois’ book Using csh and tcsh (O’Reilly). Because Unix is a multiuser system, there are two possible locations for the configuration files: one applies to all users of the system and another to each individual user.

The system-wide setup files that are read by tcsh , the default shell for Mac OS X, are found in /etc (csh.login, csh.cshrc, and csh.logout). You only have permission to change these system-wide files if you use sudo (see Section 2.3). However, you can create an additional file called .tcshrc in your home directory that will add additional commands to be executed whenever you start a new Terminal window. (If you configure Terminal to use another shell, such as the Bourne shell, the C shell, or the Z shell, you’ll need to set up different configuration files, which we don’t discuss.) The system-wide setup files are read first, then the user-specific ones, so commands in your .tcshrc file may override those in the system-wide files.

The .tcshrc file can contain any shell command that you want to run automatically whenever you create a new Terminal. Some typical examples include changing the shell prompt, setting environment variables (values that control the operation of other Unix utilities), setting aliases, or adding to the search path (where the shell searches for programs to be run). A .tcshrc file could look like this:

set prompt="%/ %h% "
setenv LESS 'eMq'
alias desktop "cd ~/Desktop"
date

This sample .tcshrc file issues the following commands:

  • The line with set prompt tells the shell to use a different prompt than the standard one. We’ll explain the details of prompt setting in Section 4.2.1 later in this chapter.

  • The line with setenv LESS tells the less program which options you want to set every time you use it. Not all commands recognize environment variables, but for those that do, this saves you the trouble of typing the options on every less command line. Environment variables have many uses within Unix. A good place to learn more about them is the book Unix Power Tools, by Jerry Peek, Tim O’Reilly, and Mike Loukides (O’Reilly).

  • The line that begins with alias defines a new, custom command that your shell will recognize just as if it were a built-in Unix command. Aliases are a great way to save shorthand names for long, complicated Unix command lines, or even to fix common mistakes you might make when typing command lines. This particular alias creates a command for going right to the Desktop directory. We give a brief tutorial on creating aliases later in this chapter.

  • The date line simply runs the date command to print the time and date when you open a new Terminal window. You probably don’t want to do this, but we want you to understand that you can put in any command that you could type at the shell prompt, and have it automatically executed whenever a new shell starts up.

By default, the .tcshrc file doesn’t exist in your home directory. Only the system-wide configuration files are read. But if you create the file, it will be read and its contents executed the next time you start a shell. (You can also create a file called .logout to contain commands to be executed when you end a shell.) You can create or change these files with a text editor, such as vi (see Section 3.3.2). Don’t use a word processor that breaks long lines or puts special nontext codes into the file. Any changes you make to these files will take effect when you open a new Terminal window. Unfortunately, it’s not always easy to know which shell setup file you should change.[5] And an editing mistake in your shell setup file can interfere with the normal startup of the Terminal window. We suggest that beginners get help from experienced users, and don’t make changes to these files at all if you’re about to do some critical work with your account, unless there’s some reason you have to make the changes immediately.

You can execute any customization command we show you here from the command line as well. In this case, the changes are in effect only until you close that window or quit Terminal.

For example, to change the default options for less so it will clear the Terminal window before it shows each new page of text, you could add the -c option to the LESS environment variable. The command would look something like this:

% setenv LESS 'eMqc'

(If you don’t want some of the less options we’ve shown, you could leave those letters out.) Unix has many other configuration commands to learn about; the sources listed in Chapter 10 can help.

Just as you can execute the setup commands from the command line, the converse is true: any command that you can execute from the command line can be executed automatically when you log in by placing it in your setup file. (Running interactive commands such as vi or ftp from your setup file isn’t a good idea, though.)

Changing Your Prompt

The easiest customization you can perform is to change your prompt. By default, tcsh on Mac OS X has a shell prompt made up of your computer hostname, your account name, and a percent sign (for example: [dsl-132:~] taylor%). If you’d rather have something else, it’s time to edit your own .tcshrc file. Use the vi editor (you might need to flip back to Section 3.3.2) to create a file called .tcshrc in your home directory (/Users/yourname) and add the following to the end of the file: set prompt="% ". You can also change the prompt for a single session by invoking the command as follows:

[dsl-132:~]taylor% set prompt="% "
%

This command will give you a simple, spare % prompt with nothing else. (The % is traditional for shells derived from the Berkeley Unix C Shell, while $ is traditional for shells derived from the original Bell Labs Bourne Shell.) It’s not necessary — you could use a colon, a greater-than sign, or any other prompt character — but it is a nice convention, because it will immediately tell an advanced user what kind of shell you are using.

If that’s all you could do to set your prompt, it wouldn’t be very interesting, though. There are a number of special character sequences that, when used to define the prompt, cause the shell to print out various bits of useful data. Table 4-1 shows a partial list of these special character sequences for fine tuning your prompt.

Table 4-1. Favorite percent escape sequences for tcsh prompts

Value

Meaning

%/

The current working directory

%~

The current working directory, with your home represented as ~ and other users’ homes represented as ~user

%c

The trailing element of the current working directory, with ~ substitution

%h

The current command history number

%M

The full hostname

%m

The hostname up to the first dot

%B/%b

Start/stop bold mode (matches the bold color in the Terminal color preferences)

%t

Time of day in 12-hour (a.m./p.m.) format

%T

Time of day in 24-hour format

%n

The username

Experiment and see what you can create that will meet your needs and be fun too. For many years, a popular Unix prompt was:

% set prompt="Yes, Master? "

It might be a bit obsequious, but on the other hand, how many people in your life call you “Master”?

One prompt sequence that we like is:

% set prompt="%/ %h% "

This prompt sequence shows the current working directory, followed by a space, and the current history number, and then a percent sign to remind the user that this is csh or tcsh. For example, the prompt might read:

               /Users/taylor 55%

This tells me immediately that /Users/taylor is my current directory, and that I’m on the 55th command I’ve executed. (Because you can use the arrow keys to scroll back to previous commands, as described in Section 1.1.3, this is no longer as important, but there is a very powerful command history syntax built into tcsh that allows you to recall a previous command by number. If you’re familiar with this syntax, making the command history number part of the prompt can be handy.) On multiuser systems, it’s not a bad idea to put the username into the prompt as well, so you always know who the system thinks you are.

Creating Aliases

The flexibility of Unix is simultaneously its greatest strength and downfall; the operating system can do just about anything you can imagine (the command-line interface is certainly far more flexible than Aqua, the graphical interface!) but it’s very difficult to remember every single flag to every command. That’s where shell aliases can be a real boon. A shell alias is a simple mechanism that lets you create your own command names that act exactly as you desire.

For example, we really like the -a flag to be included every time we list a directory with ls, so we created an alias:

% alias ls      "/bin/ls -a"

This indicates that each time we type ls in the shell, the /bin/ls command is going to be run, and it’s going to automatically have the -a flag specified. To have this available in your next session, make sure you remember to add the alias to your .tcshrc file too.

You can also have aliases that let you jump quickly to common locations, a particularly helpful trick when in Mac OS X:

% alias desktop "cd ~/Desktop"

Chapter 3 described the cp, mv, and rm commands. Each of these support the -i switch, which will prompt you before overwriting or deleting a file. You can use aliases to always enable this switch:

% alias rm "rm -i"
% alias cp "cp -i"
% alias mv "mv -i"

You can list active aliases all by typing alias without any arguments:

% alias
cp      cp -i
desktop cd ~/Desktop
ls      /bin/ls -a
m2u     (tr  '\015' '\012' )
mv      mv -i
rm      rm -i
u2m     (tr  '\012' '\015' )

Have an alias you really want to omit? You can use unalias for that. For example, unalias ls would remove the -a flag addition.

Setting the Terminal Title

You can change the current Terminal title using the following cryptic sequence of characters:

echo '^[]2;My-Window-Title^G'

To type the ^[ characters in tcsh, use the key sequence Control-V Escape (press Control-V and release, then press the Escape key). To type ^G, use Control-V Control-G. The vi editor supports the same key sequence.

Such cryptic sequences of characters are called ANSI escape sequences . An ANSI escape sequence is a special command that manipulates some characteristic of the Terminal, such as its title. ^[ is the ASCII ESC character (which begins the sequence), and ^G is the ASCII BEL character. (The BEL character is used to ring the Terminal bell, but in this context, it terminates the escape sequence.)

You can capture this escape sequence in a shell alias:

% alias settitle 'echo -n "^[]2;\!*^G"'

Then you can change the title by issuing this command:

% settitle your fancy title here

Using AppleScript to Manipulate the Terminal

AppleScript is a powerful programming language used to automate Mac OS X applications. The Mac OS X Terminal is one such application. You can run AppleScript commands at the shell prompt using the osascript utility. The \ character tells the shell that you want to enter a single command on several lines (when you use this, the shell will prompt you with a ? character):

osascript -e \
'tell app "Terminal" to set option of first window to value'

For example, to minimize your current Terminal window:

% osascript -e \
? 'tell app "Terminal" to set miniaturized of first window to true'
%

For a complete list of properties you can manipulate with AppleScript, open the Script Editor (/Applications/AppleScript) and select File Open Dictionary. Open the Terminal dictionary and examine the properties available under window. If a property is marked [r/o], it is read-only, which means you can’t modify it on the fly.



[5] In addition to .tcshrc, the shell will also read and execute files called .login, .cshrc, and .logout. Some files are read by login shells and others by non-login shells. Some are read by subshells, others aren’t. Some terminal windows open login shells, others don’t. And if you’re using the Bourne shell, still other files, such as .profile, are read instead. We focus only on .tcshrc here, but a more advanced Unix book can provide more information once you need it.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required