Mac OS X Snow Leopard: The Missing Manual (Missing Manuals)

 <  Day Day Up  >  

16.1. Terminal

The keyhole into Mac OS X's Unix innards is a program called Terminal, which sits in your Applications Utilities folder (see Figure 16-2). Terminal is named after the terminals (computers that consist of only a monitor and keyboard) that tap into the mainframe computers at universities and corporations. In the same way, Terminal is just a window that passes along messages to and from the Mac's brain.

The first time you open Terminal, you'll notice that there's not much in its window except the date and time of your last login, a welcome message, and the command line prompt (Figure 16-2).

UP TO SPEED

Mac OS X's Unix Roots

In 1969, Bell Labs programmer Ken Thompson found himself with some spare time after his main project, an operating system called Multics, was canceled . Bell Labs had withdrawn from the expensive project, disappointed with the results after four years of work.

But Thompson still thought the project ”an OS that worked well as a cooperative software-development environment ”was a promising idea. Eventually, he and colleague Dennis Ritchie came up with the OS that would soon be called Unix (a pun on Multics). Bell Labs saw the value of Unix, agreed to support further development, and became the first corporation to adopt it.

In the age when Thompson and Ritchie started their work on Unix, most programmers wrote code that would work on only one kind of computer (or even one computer model ). Unix, however, was one of the first portable operating systems; its programs could run on different kinds of computers without having to be completely rewritten. That's because Thompson and Ritchie wrote Unix using a new programming language of their own invention called C .

In a language like C, programmers need only write their code once. After that, a software Cuisinart called a compiler can convert the newly hatched software into the form a particular computer model can understand.

Unix soon found its way into labs and, thanks to AT&T's low academic licensing fees, universities around the world. Programmers all over the world added to the source code, fixed bugs , and then passed those modifications around.

In the mid-1970s, the University of California at Berkeley became the site of especially intense Unix development. Students and faculty there improved the Unix kernel (the central, essential part of the OS), added features, and wrote new Unix applications. By 1977, they had enough additional software to release their own version of Unix, the first of several Berkeley Software Distribution (BSD) versions.

As it happened , the government's Defense Advanced Research Projects Agency (DARPA) was seeking a uniform, portable OS to use for their growing wide-area network, originally called ARPAnet (and now called the Internet).

DARPA liked Unix and agreed to sponsor further research at Berkeley. In January 1983, DARPA changed ARPAnet's networking protocol to TCP/IP ”and the Internet was born, running mostly on Unix machines.

Cut to 1985. Steve Jobs left Apple to start NeXT Computer, whose NextStep operating system was based on BSD Unix. When Apple bought NeXT in 1996, Jobs, NextStep (eventually renamed OpenStep), and its Terminal program came along with it. The Unix that beats within Mac OS X's heart is just the latest resting place for the OS that Jobs's team developed at NeXT .

So the next time you hear Apple talk about its "new" operating system, remember that its underlying technology is actually over 30 years old.

For user -friendliness fans, Terminal doesn't get off to a very good start; this prompt looks about as technical as computers get. It breaks down like this:

  • office-mac : is the name of your Mac (at least, as Unix thinks of it). It's usually the Mac's Computer Name (as it appears in the Sharing panel of System Preferences), but it's occasionally the name your Mac goes by on the Internet.

  • ~ . The next part of the prompt indicates what folder you're "in" (see Figure 16-1). It denotes the working directory ”that is, the currently open folder. (Remember, there are no icons in Unix.) Essentially, this notation tells you where you are as you navigate your machine.

    The very first time you try out Terminal, the working directory is set to the symbol ~. That tilde symbol is important shorthand; it means "your own Home folder." It's what you see the first time you start up Terminal, but you'll soon be seeing the names of other folders here ” office-mac: /Users or office-mac: /System/Library , for example. (More on this slash notation on Section 1.2.2.)


    Note: Before Apple came up with the user-friendly term folder to represent an electronic holding tank for files, folders were called directories . In this chapter and the next, you'll encounter the term directory almost exclusively. In any discussion of Unix, "directory" is simply the correct term.Besides, using a term like "working folder " within earshot of Unix geeks is likely to get you lynched.

    Figure 16-2. On the Web, Mac OS X's Terminal is one of the most often discussed elements of Mac OS X. Dozens of step-by-step tutorials for performing certain tasks circulate online, usually without much annotation as to why you're typing what you're typing. As you read this chapter, remember that capitalization matters in Terminal, even though it doesn't in the Finder. As far as most Unix commands are concerned , Hello and hello are two different things.

  • chris$ begins with your short user name. It reflects whoever's logged into the shell (see the box on the facing page), which is usually whoever's logged into the Mac at the moment. As for the $ sign, think of it as a colon . In fact, think of the whole prompt shown in Figure 16-2 as Unix's way of asking, "OK, Chris, I'm listening. What's your pleasure ?"

Unless you've fiddled with Terminal's preferences, the insertion point looks like a tall rectangle at the end of the command line. It trots along to the right as you type.

16.1.1. Unix Programs

An enormous number of programs have been written for Unix. And thanks to thousands of open-source developers ”programmers all over the world who collaborate and make their work available for the next round of modification ”much of this software is freely available to all, including Mac OS X users.

Each Unix command generally calls up a single application (or process , as geeks call it) that launches, performs a task, and closes . Many of the best-known such applications come with Mac OS X.

Here's a fun one: Just type uptime and press Enter or Return. (That's how you run a Unix program: Type its name and press Enter.) On the next line, Terminal shows you how long your Mac has been turned on continuously. It shows you something like: "6:00PM up 8 days, 15:04, 1 user, load averages: 1.24, 1.37, 1.45" ”meaning your Mac has been running for 8 days, 15 hours nonstop.

POWER USERS' CLINIC

bash, Terminal, and Shells

One Unix program runs automatically when you open a Terminal window: bash . It's Apple's chosen shell for Mac OS X 10.4.

A shell is a Unix program that interprets the commands you've typed, passes them to the kernel (the operating system's brain), and then shows you the kernel's response.

But more practically, the shell is the Unix Finder. It's the program that lets you navigate the contents of your hard drive, see what's inside certain folders, launch programs and documents, and so on.

There are actually several different shells available in Unix, each with slightly different command syntax. All of the popular ones ”like tcsh, ksh , and zsh ”come with Mac OS X. (You can choose among them as your default shell using the NetInfo Manager program in your Applications Utilities folder. Click users in the first column, your account in the second, click the padlock and input your password, and then type the new shell's path into the shell text box.) But on a clean installation of Tiger, Terminal comes set to use bash .

Bash evolved from the orignal sh shell, which was named the Bourne shell after its inventor . Bash got its name, then, as the Bourne Again Shell (get it?).

So why did Apple switch to bash for Panther and Tiger? (In Mac OS X 10.2 and earlier, tcsh was the standard shell. In fact, if you upgraded from 10.2 to 10.4 rather than performing a clean install and creating a fresh account for yourself, you'll still have tcsh as your default shell.)

Turns out bash has better international-text features, among other improvements ”and goodness knows , Apple would love for Mac OS X to become a hit in as many countries as possible. Bash is also more popular than tcsh . And, of course, the bash prompt symbol, $ , is a lot more inspiring than the tcsh prompt, which was a % symbol.

You can open additional Terminal windows (45 or more, depending on how many other programs are running) by choosing File New Shell. Each window runs independently of any others. For proof, try opening several windows and then running the cal command in each.

You're finished running the uptime program. The $ prompt returns, suggesting that Terminal is ready for whatever you throw at it next.

Try this one: Type cal at the prompt, and then press Enter. Unix promptly spits out a calendar of the current month.

office-mac:~ chris$ cal May 2005 S M Tu W Th F S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 office-mac:~ chris$

This time, try typing cal 6 2005, cal -y , or cal -yj . These three commands make Unix generate a calendar of June 2005, a calendar of the current year, and a Julian calendar of the current year, respectively.


Tip: The mouse isn't very useful at the command line. You generally move the cursor only with the left and right arrow keys. (The Delete key works as it always does.)You can use the mouse, however, to select text from anywhere in the window (or other programs) and paste it in at the prompt. You can also use the mouse to drag an icon off your desktop into the Terminal window, as shown in Figure 16-3. And in the Terminal Window Settings Emulation dialog box, you'll find a preference that lets you Option-click in a command line to plant the insertion point there.
UP TO SPEED

Pathnames 101

In many ways, browsing the contents of your hard drive using Terminal is just like doing so with the Finder. You start with a folder, and move down into its subfolders , or up into its parent folders.

In this chapter and the next, you'll be asked to specify a certain file or folder in this tree of folders. But you can't see their icons from the command line. So how are you supposed to identify the file or folder you want?

By typing its pathname . The pathname is a string of folder names, something like a map, that takes you from the root level to the next nested folder, to the next, and so on. (The root level is, for learning-Unix purposes, the equivalent of your main hard drive window. It's represented in Unix by a single slash. The phrase /Users , in other words, means "the Users folder in my hard drive window" ”or, in Unix terms, "the Users directory at the root level.")

One way to refer to the Documents folder in your own Home folder, for example, would be /Users/chris/Documents (if your name is Chris, that is).

 <  Day Day Up  >  

Категории