To get into the Unix environment, launch the Terminal application (go to 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 2-1.
Figure 2-1. The Terminal window
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.
You can also have a number of different Terminal windows open if that helps your workflow. Simply use -N to open each one, and -~ to cycle between them without removing your hands from the keyboard.
If you have material in your scroll buffer you want to find, use -F (or select Find Find from the Edit menu) and enter the specific text. -G (Find Next) lets you search down the scroll buffer for the next occurrence, and -D (Find Previous) lets you search up the scroll buffer for the previous occurrence. You can also search for material by highlighting a passage, entering -E (Use Selection for Find), or jumping to the selected material with -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 2-2.
Figure 2-2. Command sequences accessible from the Scrollback menu
There are some symbols in the Scrollback menu you might not have seen before in your Mac OS X exploration: the upward facing diagonal arrow for Scroll to Top is the Top or Home key on your keyboard, and the downward facing diagonal arrow for Scroll to Bottom is the End key. You can move up a page with the Page Up key, and down a page with the Page Down key. To move up or down lines, use -Up Arrow or -Down Arrow, as needed.
Inside the Terminal window, you're working with a program called a shell . 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 is called bash (it used to be tcsh in previous versions of Mac OS X). Other available shells include the Bourne shell ( sh ), the C shell ( csh ), the Tabbed C shell ( tcsh ), 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 1.2 in Chapter 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.
2.1.1 The Shell Prompt
When the system is ready to run a command, the shell outputs a prompt to tell you that you can enter a command.
The default prompt in bash 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 by ~ , Unix's shorthand for your home directory), your login name, and a dollar 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 showed you how to customize your prompt in Chapter 1.
A prompt that ends with a hash mark ( # ) usually means you're logged in as the superuser . 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 you 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 (File Log Out) and logging back in as yourself.
2.1.2 Entering a Command
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 2.2 later in this chapter.
An example of a single-word command is date . Entering the command date displays the current date and time:
$ date Tue Sep 23 12:57:06 MDT 2003 $
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.
Another simple command is who . It displays a list of each logged-on user 's username, terminal number, and login time. Try it now, if you'd like.
The 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 who am i . This command line consists of the command ( who , the program's name) and arguments ( am i ). (Arguments are explained in Section 2.2 later in this chapter.) For example:
$ who am i taylor ttyp1 Sep 23 16:26
The response shown in this example says that:
2.1.3 Recalling Previous Commands
Modern Unix shells remember commands 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 commands, try pressing the Up Arrow key on your keyboard. You will see the previous command after your shell prompt, just as you typed it before. Pressing the Up Arrow again recalls the previous command, and so on. Also, as you'd expect, the Down Arrow key will recall more recent commands.
To execute one of these remembered commands, just press the Return key. (Your cursor doesn't even have to be at the end of the command line.)
Once you've recalled a command, you can also edit it as necessary. If you don't want to execute any remembered commands, cancel the command shown with -. or Control-C. The next section explains both of these.
2.1.4 Correcting a Command
What if you make a mistake in a command line? Suppose you typed dare instead of date and pressed the Return key before you realized your mistake? The shell will give you an error message:
$ dare -bash: dare: 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 accidentally typing control characters that are invisible on the screen. Once the prompt returns, reenter your command.
As we said earlier (in Section 2.1), you can recall previous commands and edit command lines. Use the Up-Arrow key 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, and type in changes as needed.
If you have logged into your Macintosh remotely from another system (see Chapter 8), your keyboard may be different. The erase character differs between systems and accounts, and can be customized. The most common erase characters are:
Control-C or -. will interrupt or cancel a command, and can be used in many (but not all) cases when you want to quit what you're doing.
Other common control characters are:
2.1.5 Ending Your Session
To end a Unix session, you must exit the shell. You should not end a session just by 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 7), and closing the window could therefore interrupt the process so it won't complete. Instead, type exit at a shell prompt. The window will either close or simply not display any sort of prompt; you can then safely quit the Terminal application. If you've started a background process, you'll instead get one of the messages described in the next section.
22.214.171.124 Problem checklist
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.