With a typical Unix system, a staff person has to set up a Unix account for you before you can use it. With Mac OS X, however, the operating system installation automatically creates a default user account. The account is identified by your username , which is usually a single word or an abbreviation. Think of this account as your office — it’s your personal place in the Unix environment.
When you log in to your Mac OS X system, you’re automatically logged into your Unix account as well. In fact, your Desktop and other customized features of your Mac OS X environment have corresponding features in the Unix environment. Your files and programs can be accessed either through the Mac Finder or through a variety of Unix command-line utilities that you can reach from within Mac OS X’s Terminal window.
To get into the Unix environment, launch the Terminal application. (That’s Finder → Applications → Utilities → Terminal. If you expect to use the Terminal a lot, drag the Terminal icon from the Finder window onto the Dock. You can then launch Terminal with a single click.) Once Terminal is running, you’ll see a window like the one in Figure 1-1.
Once you have a window open and you’re typing commands, it’s helpful to know that regular Mac OS X copy and paste commands work, so it’s simple to send an email message to a colleague showing your latest Unix interaction, or to paste some text from a web page into a file you’re editing with a Unix text editor such as vi.
-~ to cycle between them without removing your hands from the keyboard.
-G (Find Next) lets you search down the scroll buffer for the next occurrence, and
-J (Jump to Selection). You can also save an entire Terminal session as a text file with File → Save Text As, and you can print the entire session with File → Print. It’s a good idea to study the key sequences shown in the Scrollback menu, as illustrated in Figure 1-2.
Inside the Terminal window, you’re working with a
program called a
The shell interprets command lines you enter, runs programs you ask
for, and generally coordinates what happens between you and the Unix
operating system. The default shell on
Mac OS X
tcsh (this could change in future
versions of Mac OS X).
Other available shells include
the Bourne shell (sh), the C shell
(csh), the Bourne-Again shell
(bash), and the Z Shell
(zsh). A popular shell on other versions of Unix
(not available by default on Mac OS X) is the Korn shell
(ksh). To change the shell that Terminal uses,
see Section 4.1.
For a beginner, differences between shells are slight. If you plan to work with Unix a lot, though, you should learn more about your shell and its special commands.
To find out which
using, run the command
. (See Section 1.1.2, later in this chapter.) The
answer, which could be something like /bin/tcsh,
is your shell’s name or pathname.
The default prompt in
is the computer name (which might be
something automatically generated, such as
dhcp-254-108, or a name you’ve
given your system), the current directory (which might be represented
~, Unix’s shorthand for your
home directory), your login name, and a percent sign. For example,
the complete prompt might look like this:
[limbo:~] taylor%. The prompt can be customized, though, so your own
shell prompt may be different. We’ll show you how to
customize your prompt in Chapter 4.
A prompt that ends with a hash mark (
means you’re logged in as the
. The superuser
doesn’t have the protections for standard users that
are built into the Unix system. If you don’t know
Unix well, you can inadvertently damage your system software when you
are logged in as the superuser. In this case, we recommend that you
stop work until you’ve found out how to access your
personal Unix account. The simplest solution is to open a new
Terminal window (File → New Shell) and work in that window.
If you’ve still got the superuser prompt, it means that someone
either logged into Mac OS X as the superuser or your shell prompt has
been customized to end with a # even when you’re not the superuser.
Try logging out of Mac OS X (
→ Log Out) and logging back in.
Entering a command line at the shell prompt tells the computer what to do. Each command line includes the name of a Unix program. When you press Return, the shell interprets your command line and executes the program.
The first word that you type at a shell prompt is always a Unix command (or program name). Like most things in Unix, program names are case sensitive; if the program name is lowercase (and most are), you must type it in lowercase. Some simple command lines have just one word, which is the program name. For more information, see Section 1.2, later in this chapter.
dateFri Nov 1 16:26:55 EST 2002 %
As you type a command line, the system simply collects your keyboard input. Pressing the Return key tells the shell that you’ve finished entering text, and it can run the program.
who program can also tell you which account is
currently using the Terminal application, in case you have multiple
user accounts on your Mac. The command line for this is
i. This command line consists of the
who, the program’s name)
and arguments (
i). (Arguments are explained in
Section 1.2 later in this
chapter.) For example:
who am itaylor ttyp1 Nov 1 16:26
The response shown in this example says that:
“I am” Taylor (actually, my username is taylor). The username is the same as the Short Name you’re asked to define when you create a new user with System Preferences → Accounts → New User.
I’m using terminal p1. (This cryptic syntax, ttyp1, is a holdover from the early days of Unix. All you need to know as a Unix beginner is that each time you open a new terminal window, the number at the end of the name gets incremented. The first one by ttyp1, the second ttyp2, and so on. The terminal ID also appears in the titlebar of the Terminal window.)
I opened a new Terminal window at 4:26 in the afternoon of November 1.
Modern Unix shells remember command lines you’ve typed previously. They can even remember commands from previous login sessions. This handy feature can save you a lot of retyping of common commands. As with many things in Unix, though, there are several different ways to do this; we don’t have room to show and explain them all. You can get more information from sources listed in Chapter 10.
After you’ve typed and executed several command lines, try pressing the up-arrow key on your keyboard. You will see the previous command line after your shell prompt, just as you typed it before. Pressing the up-arrow again recalls the previous command line, and so on. Also, as you’d expect, the down-arrow key will recall more recent command lines.
To execute one of these remembered commands, just press the Return key. (Your cursor doesn’t have to be at the end of the command line.)
.. The next section explains both of
daredare: command not found %
Don’t be too concerned about getting error messages. Sometimes you’ll get an error even if it appears that you typed the command correctly. This can be caused by typing control characters that are invisible on the screen. Once the prompt returns, reenter your command.
As we said earlier (in Section 1.1.3), you can recall previous commands and edit command lines. Use the up-arrow to recall a previous command.
To edit the command line, use the left-arrow and right-arrow keys to move your cursor to the point where you want to make a change. You can use the Delete key to erase characters to the left of the cursor.
If you have logged into your Macintosh remotely from another system (see Chapter 7), your keyboard may be different. The erase character differs between systems and accounts, and can be customized. The most common erase characters are:
Delete or Del
Signals the end of input for
some programs (such as
cat, explained in
Section 6.1.1) and returns you to a shell
prompt. If you type Control-D at a shell prompt, it may close your
current Terminal window.
end a Unix session, you must exit the Terminal. You should
not end a session by just quitting the Terminal
application or closing the terminal window. It’s
possible that you might have started a process running in the
background (see Chapter 9), and closing the window
will interrupt the process so it won’t complete.
exit at a shell prompt. The window
will either close or display
then you can then safely quit the Terminal application. If
you’ve started a background process,
you’ll instead get one of the messages in the
Problem Checklist below.
The first few times you use Mac OS X, you aren’t likely to have the following problems. But you may encounter these problems later, as you do more advanced work.
You’ve been using a subshell (a shell created by
your original Terminal shell). To end each subshell, type
exit (or just type Control-D) until the Terminal
Mac OS X and many other Unix systems have a feature called
that lets you suspend a
program temporarily while it’s running or keep it
running separately in the
“background.” One or more programs
you ran during your session has not ended but is stopped (paused) or
in the background. Enter
fg to bring each stopped
job into the foreground, then quit the program normally. (See Chapter 9 for more information.)
Terminal tries to help by not quitting when you’re in the middle of running a command. Cancel the dialog box and make sure you don’t have any commands running that you forgot about.