<<

. 20
( 69 .)



>>


Y
ou can change the directories where the shell
the PATH environment variable. When you type the
searches for commands by setting your path variable.
filename, it locates any executables stored in the new path;
Changing your path is necessary if you install new
you can use the which command to confirm this.
command-line software on your computer.
If you already have a long path value, it may be difficult to
The default path includes the directories /bin, /sbin,
retype or copy-and-paste the current path value. Instead,
/usr/bin, and /usr/sbin because they are where Mac OS X
you can add new directories to the path by typing:
stores the programs that come with the system. If you add
new programs, they may install into other directories such
set path = ($path /new-directory)
as /usr/local/bin, /usr/local/sbin, or /sw/bin. If you do not
change your path, then the only way to run those programs $path tells the shell to insert the current value of the path
is to type out the full pathname before each, as in variable before setting the value. You type the pathname of
/usr/local/bin/vim filename. the new directory after $path to append it to your path.
You can set your path by typing out a list of pathnames in a Because path is a shell variable, any value that you set lasts
set command. Because you are providing a list, you should only while the current Terminal window is open. To make
use parentheses instead of single quotes. For example: the changes to your path permanent, you can store the
command to set the path in your .tcshrc file.
set path = (/bin /sbin /usr/bin /usr/sbin
/sw/bin)

SET YOUR PATH




¤ Type a list of directories ‹ Type a list of directories
SET PATH TO DIRECTORY LIST APPEND TO YOUR PATH
within parentheses and press and a right parenthesis, and
„ Type set path = and a „ Type set path = and a
Return. press Return.
space. space.
– The shell sets your path to – The shell adds the new
¤ Type a left parenthesis,
the directory list. directories to your path.
$path, and a space.




88
6
CUSTOMIZE YOUR SHELL



EDIT YOUR .TCSHRC FILE
You can also use the example tcsh files that are located in

Y
ou can save the commands to set your prompt, your
the /usr/share/tcsh/examples directory on your computer.
aliases, your shell variables, your environment
You can add a line at the very beginning of your .tcshrc file
variables, and your path so that each time you open
as follows:
Terminal, your shell is configured to your preferences.
source /usr/share/tcsh/examples/rc
You save these commands in a special file called .tcshrc that
you create in your Home directory. The period, or dot, at
The source command tells the shell to read the commands
the beginning of the name indicates that the file normally
from a text file and run them as if they are being entered
hides from the Mac OS X Finder and from the ls
directly into the Terminal program. As a result, this
command, although you can still list it using the -a option
command in your .tcshrc file tells the shell, upon starting, to
with the ls command. It is common for Unix programs,
run the commands stored in the /usr/share/tcsh/examples/
such as the tcsh shell, to store their configuration settings in
rc file. For more information, you can type less /usr/share/
invisible dot-files in your Home directory. These files begin
tcsh/examples/README. You can also store your aliases,
with a period and often end in rc.
variables, and environment variables in separate files as
described in the README file, although it is acceptable to
The tcsh shell executes all commands in your .tcshrc file
set them all within your .tcshrc file.
whenever you open a Terminal window or start a shell. To
create your .tcshrc file, you can use any text editor, including
Pico, vi, or emacs, and enter one command per line.


EDIT YOUR .TCSHRC FILE




– When you start a new
„ Type cd ˜. ‹ Type .tcshrc and press › Type the commands to set
Return. your preferences. Terminal window, the shell
– This takes you to your executes the commands in
– The text editor opens the ˇ Save the file, and exit the
Home directory. the .tcshrc file.
file for editing. editor.
¤ Type the name of a text
editor command and a space.



89
UNIX FOR MAC



RECALL YOUR COMMAND HISTORY
typed. If you type an exclamation point and then some

Y
ou can list the commands that you have typed before
letters, the shell searches through the command history for
by using the history command. You can also use
the most recent command beginning with those letters, and
special characters to repeat or edit commands you
repeats that command. In both cases, the shell displays the
have already typed.
recalled command immediately before executing it. A list of
other history codes appears on the facing page.
The history command lists the commands you have typed
since opening this Terminal window, along with the time at
You can also edit the previous line by using a caret (^). This
which each command was run. The history command
is a method to correct a mistake you may have made, or to
lists the 100 most recent commands, provided that you have
change a command so that it applies to different files. You
typed more than 100 commands into the current Terminal
use the caret as follows:
window.
^old-text^new-text
You can repeat commands or portions of commands using
the history recall built into the shell. You do this by typing
The command is repeated, but with the old-text replaced
an exclamation point (!), or bang, followed by a special
by the new-text. For example, if you have just typed cd
code that indicates what information from your history you
/usr/shrae and you want to correct yourself, you can type
want to recall. There are a number of different history
^shrae^share and press Return.
codes that you can use, the simplest being two exclamation
points (!!); this code repeats the last command that you


RECALL YOUR COMMAND HISTORY




– The shell lists all the – The shell displays the
LIST COMMAND HISTORY REPEAT THE LAST COMMAND
commands you have typed previous command.
„ Type history and press „ Type !! and press Return.
into the current Terminal
– The shell executes the
Return.
window.
previous command.




90
6
CUSTOMIZE YOUR SHELL



The table below lists some of the most useful history
codes. You can use these history substitutions anywhere
in your command line. For example, if you have just
moved a series of files with the mv command and you
want to change their file permissions, you can type chmod
+r !!* to use the chmod command on the arguments
supplied to the mv command that you have just executed.

HISTORY CODE MEANING
The previous command.
!!
The argument of the previous command.
!!*
The first argument of the previous command.
!!^
The last argument of the previous command.
!!$
The last command beginning with text.
!text
The last command containing text.
!?text?
The command number from the history command.
!number
The command three commands ago.
!-3




– The shell executes the ‹ Type ^ and the corrected
REPEAT A SPECIFIC COMMAND CORRECT A MISTAKE IN
THE LAST COMMAND
most recent command that portion of the command.
„ Type !.
„ Mistype a command and
begins with those letters.
› Press Return.
¤ Type the first few letters of press Return.
– The shell executes the last
a previous command and
¤ Type ^ and the mistyped
press Return. command with the correction
portion of the command. in place.

91
UNIX FOR MAC



START A NEW SHELL
If you are done using a new shell, you can close it by typing

Y
ou can start another shell by typing the pathname of
the exit command. This returns you to your original shell.
the shell. Running commands in a sub-shell can
If you changed your current directory in the new shell, you
preserve your shell settings or allow you to use a
find yourself back in the original directory when the new
different shell program.
shell exits.
Each time you start a new shell, it executes the commands
Mac OS X comes with three shells installed. The tcsh shell is
stored in your .tcshrc file ” or the appropriate dot-file for
the default, and the one that this book assumes you are
the shell if it is not tcsh ” and gives you a new prompt.
using. The Bourne-again shell, known as bash, is a popular
Most shell variables are set to their initial default values, but
alternative to the tcsh. The Z shell, zsh, offers many
environment variables remain set with any values they had
customization opportunities for Unix power users. The
in the original shell.
pathnames for these shells, respectively, are /bin/tcsh,
/bin/bash, and /bin/zsh. For more information about the
When the new shell starts up, it has a clean command
bash and zsh shells, see "Work with the Bourne-Again
history. If you type history, you see a very short list that
Shell" and "Work with the Z Shell" later in this chapter.
does not include any of the commands executed in the
original shell. Likewise, after you exit the new shell, the
command history for that shell does not appear in the
history of the original shell. You only see the command to
start the newer shell.



START A NEW SHELL




– A new shell process starts. – The shell exits, returning
START A SHELL PROCESS EXIT THE NEW SHELL
you to your original shell.
„ Type the full pathname of „ Type exit and press
– The path remains what it
a shell you want to start. Return.

<<

. 20
( 69 .)



>>