BUY THIS BOOK

Safari Books Online

What is this?

Looking to Reprint this content?


Running Mac OS X Panther
Running Mac OS X Panther Inside Mac OS X's Core

By James Duncan Davidson

Cover | Table of Contents | Colophon


Table of Contents

Chapter 1: Where It All Came From
The original Mac OS defined the basic metaphors that influenced personal computing and for a decade was the standard by which all other personal computer operating systems were measured. It was not until Microsoft Windows 95 was introduced that anything else came close. Unfortunately, due to many mistakes made by Apple while Microsoft continued to improve Windows, close was good enough and Microsoft ended up owning the user desktop experience for the last half of the 1990s. Now, with the advent of a technological tour de force known as Mac OS X, the Mac is once again setting the standard for what a personal operating system should be.
The Mac OS as we now know it is far different from the one released in 1984. It is a successful hybrid of ideas that were first expressed in the original Macintosh and of technologies that come from a computing philosophy long considered to be the antithesis of the Mac experience: Unix. The fusion of these two very different camps gives us the platform we know and love today.
Instead of giving the same laundry list of features in this chapter that you can get from Apple's web site or from most other books covering Mac OS X, I'm going to take a look at the predecessors to Mac OS X and explore the various origins of the modern system. The intent isn't to tell the story of Apple Computer or of Steve Jobs, though those histories are interesting. The focus here is on the technologies at hand, where they came from, and how they influence the development of the Mac operating system. Each major technology of the current Mac OS X will be introduced and its source identified.
In 1983, after the incredible success of the original Apple II, which was the first computer that was a true personal computer for most people, and after the later relative disappointments of the Apple III and the Lisa, Steve Jobs organized a group of engineers under a pirate flag to make a computer that could be used by anybody.
The result of their efforts, known as the Macintosh, was introduced to the world a year later accompanied by the fanfare of the famous "1984" commercial (directed by Ridley Scott of
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
The Classic Mac OS
In 1983, after the incredible success of the original Apple II, which was the first computer that was a true personal computer for most people, and after the later relative disappointments of the Apple III and the Lisa, Steve Jobs organized a group of engineers under a pirate flag to make a computer that could be used by anybody.
The result of their efforts, known as the Macintosh, was introduced to the world a year later accompanied by the fanfare of the famous "1984" commercial (directed by Ridley Scott of Alien and Blade Runner fame). With a message of empowerment, the Mac redefined what a personal computer could be. What made the Mac different from every other personal computer at the time was its Graphical User Interface (GUI), inspired by research that originated at Xerox PARC: a desktop where files lived in folders, and you used a mouse to move things where you wanted them. The Mac's original desktop is shown in Figure 1-1.
Figure 1-1: The original Mac OS desktop
The desktop metaphor introduced with the original Mac in 1984 lives on today in the current Finder. Sure, today's desktop is rendered in full color and the original was comparatively crudely drawn in black and white, but the basic idea remains much the same: opening disks and folders yields windows that contain icons that represent files.
At the heart of the system was a set of programmatic routines called the Macintosh Toolbox that allowed programmers to implement the various essential interface components, such as windows, menus, alert boxes, scrollbars, and other controls, in their applications. In 1984, the Toolbox was an amazing feat of engineering and contributed to a consistent user experience that was the envy of other operating system vendors. But, over the years after its release, more and more routines were added to the Toolbox as the system developed. This resulted in more features, but those features came with a price—the system became fragile.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
System 7
In 1991 Apple introduced System 7, an upgrade that greatly modernized the Mac. It incorporated many features that would guide future development: seamless multitasking (albeit cooperative), color icons, personal file sharing, virtual memory, and a hierarchical System folder to help organize Control Panels and Extensions. The Mac interface also evolved for System 7, growing a bit more sophisticated as shown in Figure 1-2.
Figure 1-2: The System 7 desktop
System 7 also lay the foundation for three very important technologies:
QuickTime
A powerful multimedia technology for manipulating, enhancing, storing, and streaming video, graphics, sound, and animation.
ColorSync
A set of system routines for color management allowing for graphics to be color managed from editing to print.
AppleScript
An easy-to-use scripting language that allows you to automate tasks on your system.
Although System 7 was years ahead of every other operating system from the ease-of-use perspective, not all was rosy with this release. With all the new functionality, the Toolbox had become even more fragile than its predecessors. It turned out that cooperative multitasking, where applications are expected to play nice with each other, meant that any application could hog all the processor time and, in effect, lock up a system. And, most glaringly, the Mac OS did not provide memory protection between programs. This not only let applications run faster as well as enable all sorts of nifty customizations (also known as hacks), but it also let applications scribble into each other's memory space, inevitably corrupting the system. As Apple released new hardware, developers dreamed up new features that could only be implemented using system extensions. While extensions had good intentions, they frequently conflicted with each other, which forced users to reboot their Macs several times a day.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Copland
To address the need for a more stable operating system, Apple embarked on the development of a new operating system, dubbed Copland, in 1993. With Copland, the focus was on increasing stability, portability, ease-of-use, and performance. Copland was intended to be Apple's stepping stone to a future OS that would include preemptive multitasking and protected memory—keeping applications separate and preventing them from crashing each other or taking the entire system down. Much to the chagrin of Mac users as well as Apple, Copland descended into a death spiral of budget and schedule overruns.
Three years later, in 1996, Copland was cancelled and Apple desperately looked elsewhere for a new foundation for the Mac OS. For a time, it looked like Apple would purchase Be, started by Apple alum Jean-Louis Gassée, and use the technically sophisticated BeOS as the foundation for its next generation operating system. The BeOS was developed on the PowerPC chip fitting in with Apple's Mac hardware strategy, had impressive multitasking abilities, an advanced filesystem, and provided the memory protection that the Mac OS so desperately needed. The only catch: BeOS was an unfinished work-in-progress that was unproven in the marketplace.
After several months into negotiations to acquire Be, the two companies were not able to agree on a price. Be wanted $400 million dollars for its unfinished and unproven system, and Apple did not want to take that much risk for the price. After negotiations fell through late in 1996, Be struggled for several more years, eventually being acquired by Palm, Inc.
Once again, Apple was left with no direction for its operating system, and many thought that the demise of Apple was near. Then, discussions started with Steve Jobs that led to the purchase in December 1996 of the company that he founded after leaving Apple in 1985: NeXT Computer. With this purchase came the portable and feature-rich NEXTSTEP operating system.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
NEXTSTEP
Originally introduced to the world in 1987, along with the elegant NeXT cube, NEXTSTEP was intended to, "Create the next insanely great thing," as only Steve Jobs could say. The driving mantra was to do everything right, and not to repeat the mistakes that Apple had made. Built on top of BSD Unix and the Mach microkernel, NEXTSTEP had the preemptive multitasking and memory-protected core that Mac OS needed. NEXTSTEP also utilized Display PostScript from Adobe allowing developers to use the same code to display documents on screen as well as to print to paper—enabling truly WYSIWYG applications. NEXTSTEP also featured a rich GUI desktop, as shown in Figure 1-3.
For developers, NEXTSTEP came with a set of libraries, called "frameworks," and tools to enable programmers to build applications using the Objective-C language. Developers loved this mix of technologies. For example, Tim Berners-Lee used NEXTSTEP to write the first few versions of both the client browser and server software that would start the World Wide Web.
NEXTSTEP evolved through many releases and was adopted by many governments and companies as their platform of choice. It made inroads into the military, banking, healthcare, and telecommunications industries and received glowing reviews from the press. Because it was created as a fresh start, avoiding the mistakes of the design of the original Macintosh and yet building on its ideas, it was able to jump far ahead of anything else.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Rhapsody
At the 1997 World Wide Developer Conference (WWDC), Apple demonstrated an early build of Rhapsody, a version of NEXTSTEP that ran on Macintosh hardware. With NeXT's experience porting the system to multiple hardware architectures, it was easy enough for Apple to get a build up and running on a single model Mac model in time for the conference. The development community was invigorated as an ambitious project plan was announced that would supposedly lead to a release in 1998 of a new Mac OS for the PowerPC and Intel platforms.
As part of the project plan, three major components were announced: The Yellow Box, which would run OpenStep-based applications on top of Rhapsody; the Blue Box, which would run a future version of the classic Mac OS as a Rhapsody process; and the Red Box, which would allow OpenStep applications to run on top of Windows with a simple recompile.
Rhapsody brought the integration of the following technologies into the current Mac OS X:
Classic
With the outgrowth of the Blue Box, Classic allows older Mac applications to run unmodified under the new operating system. Essentially an operating system running within a process, it doesn't confer all the advantages of a protected-memory preemptive system to the applications running within it. If an application crashes inside Classic, it can corrupt any other applications also running in Classic. However, Classic allows the old and new operating systems to run at once with the same screen, mouse, and a modicum of cross-platform application utility. It's not a perfect solution, but it allows users to continue using their older Mac OS applications as they migrate to the new system.
Java
From the beginning, the Rhapsody team made it a priority to implement good Java support on the platform. This support would allow Java applications and applets to run and operate without modification. As well, a layer of functionality—known as the
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Continued Development of the Classic Mac OS
After Apple bought NeXT, not all its development efforts were spent creating Rhapsody and eventually Mac OS X. While development proceeded with the new operating system, several releases of the classic Mac OS bridged the gap between System 7 and Mac OS X.
Mac OS 8, which made its appearance on July 22, 1997, was the first of these releases. Hailed at the time as the "Most significant Macintosh operating system since 1984," it brought a new platinum-based look and feel to the desktop (based on the look and feel from Copland), complete with spring-loaded folders that popped up when a file was dragged onto them, and contextual menus activated by holding down the Control key while clicking an icon. Mac OS 8 also made it easier for users to connect to the Internet with an Internet Set Up Assistant and bundled Netscape Navigator and Microsoft Internet Explorer. Finally, Mac OS 8 marked the introduction of the Macintosh Runtime for Java, allowing applications written in the Java cross-platform language to run on the Mac.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Mac OS X 10.0
Apple shipped Mac OS X Public Beta in September 2000. More than 100,000 people bought the beta, and Apple reported that there were more than 75,000 feedback submissions. Not only did the Public Beta serve to indicate that Apple was going to ship Mac OS X, it helped Apple identify the issues that users needed to have addressed in order to replace the original Mac OS.
After four years in development, the first full release of Mac OS X, known to the engineers who worked on it as Cheetah and to the public as version 10.0, finally shipped on March 24, 2001—17 years and 3 months after the introduction of the original Macintosh. People gathered at stores everywhere to welcome its introduction. The fusion of Mac OS and Unix was complete, and Apple finally had the next-generation operating system that it wanted.
This release brought the following features:
Carbon
A subset of the original Mac OS Toolbox APIs that could be safely transitioned from the old Mac OS to the new one, Carbon was the lifeline for older Mac OS applications, allowing them to be ported to the new system. This meant that applications essential for attracting users to the new system, such as Microsoft Office and Adobe Photoshop, could be ported to Mac OS X and take advantage of preemptive multitasking and protected memory.
Quartz 2D
The powerful imaging layer of Mac OS X, Quartz 2D handles all the drawing on the system and delivers a rich imaging model based on PDF (replacing the Display PostScript used by NEXTSTEP), on-the-fly rendering, and antialiasing—all managed by ColorSync to ensure graphics look their best.
Quartz Compositor
The windowing system for the OS and provider of low-level services such as event handling and cursor management, the Quartz Compositor is based on a "videomixer" model where every pixel on the screen can be shared among windows in real time. This model allows for smooth transitions between the states of a GUI; one of the traits of the Aqua experience.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Mac OS X 10.1
The next release of Mac OS X, version 10.1 (known to Apple engineers as Puma), was released in September 2001, just six months after the initial release of the system. With Puma, Apple focused on performance and ensuring that the critical Carbon layer was robust enough for Microsoft Office and Adobe Photoshop to be released. Puma, along with the large influx of applications that came with it, made it clear that Mac OS X was going to be a success and that the strategy for migrating from the old Classic Mac OS to the new OS was going to work.
The quick release cycle of Puma also indicated that Apple was moving rapidly to improve the OS in ways that were meaningful for both Classic Mac OS users as well as those switching from other platforms like Windows. As a maintenance release, it was released as a no-charge upgrade—the only version of Mac OS X to date that didn't have a price tag associated with it.
With Puma came the release of the last of the original iApps to make the jump from OS 9: iDVD. Apple's Digital Hub strategy was now fully on Mac OS X, signalling that Apple's application development efforts were now entirely focused on Mac OS X. Apple further strengthened the iApps strategy in January 2002 with the release of iPhoto for managing digital photos, and continued in July 2002 with the release of iTunes 3 as an update to the popular music jukebox application.
Also revealed in July was .Mac (pronounced "dot-Mac"), a suite of Internet services, including those that used to be part of the older iTools service, and software. The new pay service included the McAfee Virex virus scanner and the new Backup software for making safe backups of critical data to .Mac.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Mac OS X 10.2 Jaguar
The next release took Mac OS X to the next level. Unlike previous versions of Mac OS, Apple decided to put the codename, Jaguar, into the name of the product itself. On the box and at its web site, it is known as Mac OS X Jaguar. It is also the first release of Mac OS X to come as the default boot OS on every system Apple sold, marking an important step in the transition between the classic Mac OS and Mac OS X. The Jaguar desktop is shown in Figure 1-6.
Jaguar introduced the following features:
Quartz Extreme
Building on the already powerful Quartz Compositor, Quartz Extreme performs most of its work in OpenGL, allowing it to take advantage of the enormous potential of modern graphics processors—and offloading the tasks from the computer's CPU. With Quartz Extreme, all applications on the system have easy access to 3D capabilities, and this paves the way for later user interface improvements.
Figure 1-6: The Jaguar desktop with iChat and iSync
Address Book
Previous versions of Mac OS X had a rudimentary contact database. Jaguar introduced a new system-wide contact database, along with a management application, called simply the Address Book. With its simple and elegant API, the Address Book can be incorporated into any application, and all the built-in applications on Jaguar, such as Mail and iChat, support it.
Rendezvous
Built on the work of the IETF Zero Configuration (Zeroconf) effort, Rendezvous enables machines to discover each other without user intervention. This lets you share services with ease between machines on the same network and allows you to seamlessly connect to machines by setting up a local network and just hooking up cables or setting up an ad hoc wireless network. You can find out more about ZeroConf at
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Mac OS X Panther
Panther, the name given to version 10.3 of Mac OS X and the system that this book covers, brings another host of improvements to the system and continues in the Mac OS X tradition of "It's getting better all the time." A few of the improvements made to the system are:
A new Finder
The Finder that shipped with Mac OS X 10.0 through 10.2 was a simplified interpretation of the classic Finder from the original Mac OS that was designed to work on a single-user system. On the multiuser Mac OS X, this meant the files that the typical user cared about—those in their Home directory—were located three levels away from the filesystem root.
The new Finder in Panther, shown in Figure 1-7, introduces a new Sidebar on the left-hand side of every Finder window that gives you quick access to the various disks attached to your computer, your directory, and any other locations that you put there. When you select a disk or a folder from the Sidebar, it becomes the start of what you see in the window view. And, changing a paradigm in place since 1984, removable disks can be ejected or disconnected by clicking on an eject icon in the Finder's Sidebar, rather than having to drag disks to the Trash.
Exposé
Building on the power of Quartz Extreme, Exposé introduces a new way to work with the dozens of windows that are open on the typical Mac OS X user's desktop. Instead of having to dig through the open windows one by one, you can use either a keystroke or a mouse gesture and perform one of three actions:
  • Show all the windows open in miniature so you can see all of them at once.
  • Show all the windows belonging to a particular application.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
What Does the Future Hold?
Without a doubt, Apple has many more features in store for future versions of Mac OS X. There are many at Apple who say building on top of Unix has allowed the company to innovate faster than expected—and with features like Exposé building on top of Quartz Extreme I'd have to agree. So, even though nobody outside Apple's headquarters in Cupertino has a clue as to what may be coming up next, it is certain that Mac OS X will continue to break new ground. Apple's choice to use Unix—a well understood, stable, and time-tested design—as the foundation of the system means Apple can focus on innovating above Unix and continue to bring improvements in the way that we use the system.
Whatever plans Apple has, it has surely noticed that Microsoft adopted several of Jaguar's features into Windows XP and that the next version of Windows—codenamed Longhorn—has a feature list that borrows many more features from Panther. Whatever Apple has up its sleeve, it's safe to say that the company is aware of this and fully intends to leapfrog Longhorn even before it finally shows up.
Enough about the past and the future. Let's look at what you can do with Panther today!
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Chapter 2: Lay of the Land
When you first log in to Mac OS X you see a user interface that is the end result of 20 years of development; quite a bit of work has gone into making it an elegant and usable interface for your computer. Panther's new Finder goes even further, bending the computer's view of the world to your own, allowing you to view the filesystem any way you want to. Underneath it all, however, Mac OS X is a structured environment based on Unix and to truly master it you'll need to know how it all fits together.
To navigate the system, you use two programs: The GUI-based Finder, and the command- line-based Terminal. Each of these programs gives access to different layers of the system. For some tasks, the Finder is the best tool for the job. For others, using the Terminal is a way of life.
This chapter explains how the filesystem in Mac OS X is put together and shows you how to navigate your way through it using the Finder and the Terminal.
Like most other filesystems, the Mac OS X filesystem is conceptually a hierarchical tree-based structure that branches from a root. In the case of Mac OS X, the filesystem is rooted on the drive partition that the system booted from. In the typical case where you boot off the single partition of the hard drive that is in your machine, your filesystem is rooted there. If you have more than one disk drive, then the filesystem is rooted from whichever partition you booted from. Likewise, if you boot from an external disk drive or even a disk image on a network server, the filesystem will be rooted from there. Figure 2-1 shows a Finder window displaying the boot disk's filesystem.
The structure of the Mac OS X filesystem at the root level is very strict, and almost every file that Mac OS X needs to run has a specific place within it. Some of the folders at the root of the filesystem are visible in the Finder when you click on your boot drive, others are not.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Filesystem Hierarchy
Like most other filesystems, the Mac OS X filesystem is conceptually a hierarchical tree-based structure that branches from a root. In the case of Mac OS X, the filesystem is rooted on the drive partition that the system booted from. In the typical case where you boot off the single partition of the hard drive that is in your machine, your filesystem is rooted there. If you have more than one disk drive, then the filesystem is rooted from whichever partition you booted from. Likewise, if you boot from an external disk drive or even a disk image on a network server, the filesystem will be rooted from there. Figure 2-1 shows a Finder window displaying the boot disk's filesystem.
The structure of the Mac OS X filesystem at the root level is very strict, and almost every file that Mac OS X needs to run has a specific place within it. Some of the folders at the root of the filesystem are visible in the Finder when you click on your boot drive, others are not.
Figure 2-1: The Finder showing the boot disk
The folders at the root of the filesystem that are visible in the Finder are:
Applications
This folder contains applications that are available to all users on the system. Most applications that Apple ships for Mac OS X (such as iCal, iPhoto, and Safari) are located here. In addition, a large number of useful utility programs, such as the Terminal and Activity Monitor, are installed in the Utilities subfolder.
Developer
If you have installed the developer tools on your system, most of the applications, documentation, examples, and other files needed for building applications for Mac OS X are located here.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
The Many Roots of the Finder
Panther's new Finder allows you to ignore the way the filesystem is structured at the command-line level and look at it in a variety of ways that are more relevant to everyday tasks. By selecting one of the items in the Sidebar (shown in Figure 2-3), the view to the right of the Sidebar changes to reveal that folder's contents, or specific details about a selected file.
Figure 2-3: The Finder's sidebar
The most useful feature about this new tool in the Finder is that it gives you quick access to your Home folder (which is usually the center of activity on your computer), or to any other folder that you place in the Sidebar.
All the files, applications, preferences, and resources that are yours and yours alone are located within your Home folder. This is where you should make all your modifications and additions. If you are an old-school Mac OS 9 user, this is where you should feel like customizing your system. And, even better, if you play by the rules, you'll be able to move to a new machine simply by copying your Home folder.
The folders you'll find inside your Home folder are:
Desktop
Contains all the files and folders that appear on the Mac OS X desktop for the user.
Documents
Intended to contain your documents. Of course, you can save your documents anywhere in your Home folder, but this is the recommended location. It is also the default location that will be proposed for you when you save a document from an application.
Library
Contains application- and user-specific resources that belong to a single user only. This allows you to have fonts on the system that nobody else can use. It also allows your applications to save your preferences separate from those of other users.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Filesystem Domains
In our discussion about the filesystem, you've no doubt identified a certain amount of redundancy. There's a Library folder at the root of the filesystem, one in your Home folder, and one in the System folder. And if you create an Applications folder in your Home folder to store applications that aren't for use by others, you'll note that it automatically gets the same folder icon as the Applications folder at the root of the filesystem, as shown in Figure 2-4.
Figure 2-4: The Local and User domains in the Finder
This is the result of a concept known as Filesystem Domains and is structured in such a way as to allow multiple users to share the same system or to be hosted on a server so they can use multiple systems and yet provide a consistent experience.
There are four domains in Mac OS X:
User
Contains the resources for a user logged in to the system. As you would expect from the similarity of descriptions, this domain is contained within the user's Home folder.
Local
Contains the resources that users of a particular system share with each other. The local domain consists of the Applications and Library folders at the root of the filesystem. These resources are available to users of the system but are not available to users on networked computers.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
The Library
I've made lots of references to the Library directory and have indicated that it is for the storage of resources. Any kind of resource that an application needs can be located in the Library. Here's a list of some of the most common directories you'll find in the Library and the kinds of resources they contain (and remember, you can create one of these directories in a domain in which it doesn't exist):
Application Support
Contains third-party plug-ins, helper applications, templates, and even data for the applications on your system. You'll usually find the resources for an application in a subdirectory named after the application.
Audio
Contains drivers, plug-ins, and sounds for Mac OS X's audio subsystems.
Caches
Contains temporary data used by the applications on your system. For example, Safari will keep web page data here so when you revisit a page, you don't have to download all the content on it again.
Calendars
Contains the ICS files used to store the calendar data for a user. This directory is used by iCal and only appears in the User domain.
ColorSync
Contains profiles and scripts used by Mac OS X's ColorSync color management subsystem.
Documentation
Contains documentation for various parts of the system. Also, the Help application uses this folder to hold files that it displays.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Further Explorations
A great resource for expanding your understanding of the way in which the system is put together is installed on your hard drive when you install Xcode Tools. It's a 191-page PDF book titled System Overview and is located at /Developer/Documentation/MacOSX/Conceptual/SystemOverview/SystemOverview.pdf.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Chapter 3: The Terminal and Shell
The Terminal application (/Applications/Utilities) is the portal to the internals of Mac OS X. You can use, and become proficient with, the system without ever touching the Terminal. But if you truly want to dig deep and learn how to unleash the full potential of the underlying Unix capabilities of the system, the command line is essential. And once you know how to use it, the Terminal becomes a tool so valuable that many power users keep it in their Dock or in the Finder's sidebar for quick access.
Tempting as it may be to think of the Terminal as the Unix part of Mac OS X, it's simply an interface to the underlying Unix operating system, and specifically to those programs that give the system its Unix character.
I'm going to make the assumption that you have at least a passing familiarity with the idea of the command line. Maybe you remember using a shared system at a school somewhere. Or maybe you had a DOS-based machine that required you to go sleuthing into the depths of the C:\ world. In any case, the aim of this chapter is to familiarize you with the Terminal, the shell, and some of the other tools you'll need through the rest of the book.
If this chapter goes over your head and you need more of a grounding on the subject, you should pick up Learning Unix for Mac OS X Panther, by David Taylor, et al. (O'Reilly & Associates, Inc., 2004).
When you launch the Terminal application, shown in Figure 3-1, you are greeted with a single, rather plain-looking window. What you see here is essentially the same command- line interface that Unix users have been seeing since the days when 8-track tapes were en vogue. The system greets you with a friendly "Welcome to Darwin!" message and then gives you a prompt for the bash shell, indicated by the dollar sign ($). This is your cue that you are interacting with the command line. At this point, you have direct access to the internals of Panther, and if you have administrator privileges and a little bit of know-how, you can do everything you ever wanted to, and more.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Terminal Overview
When you launch the Terminal application, shown in Figure 3-1, you are greeted with a single, rather plain-looking window. What you see here is essentially the same command- line interface that Unix users have been seeing since the days when 8-track tapes were en vogue. The system greets you with a friendly "Welcome to Darwin!" message and then gives you a prompt for the bash shell, indicated by the dollar sign ($). This is your cue that you are interacting with the command line. At this point, you have direct access to the internals of Panther, and if you have administrator privileges and a little bit of know-how, you can do everything you ever wanted to, and more.
Figure 3-1: The Terminal window
The prompt that is being displayed in the Terminal is the output from a program known as the shell. The shell is the mediator between you and the internals of the Unix system. The shell's job is to interpret the commands you type and invoke the various programs on your system to satisfy those instructions. Each command you enter typically consists of a program name and some parameters to pass to that program. When the shell executes the program, its result are displayed in the Terminal window. Example 3-1 shows the use of the date command, which returns the current date and time.
Example 3-1. Using the date command
$ date
Mon Dec 8 18:25:05 PST 2003
		
After the date command exits, the shell resumes control of the display and outputs another prompt enabling you to enter your next command.
In the Finder, it's easy to find your way around; after all, it was designed to be easy. The command line isn't quite such a walk in the park. It's just you and the prompt. So, how do you find your way around? Quite simply by using a set of commands at the prompt. Table 3-1 lists the commands that you will most often use to navigate your way around.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Mac-specific Shell Commands
Although Mac OS X is a Unix system, its filesystem contains some features over and beyond what the Unix cp and mv commands can handle. Apple has made it a priority to integrate the GUI and command-line worlds by including several tools.
The open command is the Terminal version of double-clicking a file or an application. Its syntax is:
  open -a application filename
			            
For example, to open the ~/Sites/index.html file in the default browser on the system (typically Safari), use the following:
  $ open ~/Sites/index.html
			            
When you execute this command, the system will find the appropriate application to open index.html with. However, if want to specify another application to open the file with, such as BBEdit (www.barebones.com) so that you can edit the HTML, you can use the -a option as shown:
  $ open -a Safari ~/Sites/index.html
			            
One thing to keep in mind about the open command is that the application is free to interpret the files passed to it however it wants to. For example, if you use the following command:
  $ open -a Safari ~/Sites
			            
Safari will launch, see that ~/Sites is a directory, and then use its default behavior for local directories, which is to cause it to be opened up in the Finder.
If you want to open an application from the command line, you don't have to specify a filename. Just execute the command like this:
  $ open -a Safari
			            
If you have Safari set to open a web site at startup, that page will load if your Mac is connected to the Internet after Safari launches.
The cp and mv commands know how to deal with plain files, but because they were designed for Unix, they don't know about some of the special attributes that Mac applications can associate with files. Most of the time, these attributes, known as
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Configuring and Using bash
As mentioned previously, with the Panther release of Mac OS X, bash is the default shell. Apple made the switch from tcsh to bash because of its support for Unicode text, something that's very important in the international market. Another logical reason for switching to bash is that it is the default shell for most Linux distributions and is easier to script with than tcsh. Because bash is now the default shell, the book will focus on its use with an occasional nod to the other shells where appropriate.
The default configuration of bash is perfectly adequate for casual usage, but you'll inevitably want to configure it to your own liking. This section takes a look at the various configuration files for bash, its environment variables, how to set up command aliases, and how to use bash's history to your advantage.
Every program on the system runs in an environment. The environment consists of a set of name-value pairs, known as environment variables, which communicate a variety of configuration settings to a program. For example, the shell uses the PATH environment variable to find a program to execute in response to a command. To get an idea of what kinds of data are stored in environment variables, execute the set command, as shown in Example 3-6.
Example 3-6. Examining environment variables with set
$ set
BASH=/bin/bash
BASH_VERSINFO=([0]="2" [1]="05b" [2]="0" [3]="1" [4]="release" [5]="powerpc-appledarwin7.0")
BASH_VERSION='2.05b.0(1)-release'
COLUMNS=80
DIRSTACK=()
EDITOR=/Users/duncan/bin/usebbedit
EUID=501
GROUPS=()
HISTFILE=/Users/duncan/.bash_history
HISTFILESIZE=500
HISTSIZE=500
HOME=/Users/duncan
HOSTNAME=Incognita.local
HOSTTYPE=powerpc
...		
When you execute the set command, you'll see quite a bit of output—probably around 40 lines. Some of the environment variables may make sense when you first look at them, and some won't. Table 3-5 lists some of the more commonly-used environment variables you are likely to use on occasion.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Using Other Shells
Essentially, all the shells on the system (sh, bash, tcsh, and zsh) do the same thing: they take input from you and translate it into commands that are run on the system. However, each shell differs in the specifics of how you interact with it and the special features that it offers. If you've never used the Unix shell, you'll do just fine sticking with the default bash shell. But if you've become accustomed to the tcsh shell that was the default on previous versions of Mac OS X, or if you are an old-hand Unix user who simply prefers another shell, you may want to change the default shell to suit your preferences.
Because a shell is a program like any other, all you have to do to use a different shell temporarily is to type its name at the command line. For example, if you want to use the tcsh shell, execute the following:
  $ tcsh
		          
The prompt will change to a percent sign (%), which is the default prompt for the tcsh shell, and will be in effect until you exit out of it.
To change the default shell used by the Terminal when it launches, simply open the Terminal preferences (Terminal Preferences), then specify the shell you want to execute in the "Execute this command" text field, as shown in Figure 3-3. Now, whenever you open a Terminal window, you'll get the shell that you want.
Figure 3-3: Changing the shell using the Terminal Preferences pane
Note that this setting doesn't change your default shell when you remotely log in. To change your default shell at the system level rather than only in the Terminal application, you'll need to modify your user account using NetInfo as shown in Chapter 5.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Shell Scripts
One of the true powers of the shell is that you don't have to always type in your commands by hand tediously one after the other. When you find a set of tasks that you do all of the time, you can consolidate them into a shell script. A shell script is nothing more than a collection of shell commands that is saved in a file on the filesystem. The shell script is made into an executable by changing its file permissions using chmod +x. A simple shell script is shown in Example 3-13.
Example 3-13. A simple shell script
#!/bin/sh
echo Hello World!
		
The first line of a shell script is special and indicates to the system that it is to be executed with the /bin/sh program. The first line always starts with the #! characters (known to Unix hands as the shebang line) followed by the name of the tool to execute the script with. For example, if you wanted to write a shell script using tcsh, the first line would read #!/bin/tsch. For most purposes it's best to write shell scripts using either the sh or bash shells. Writing to the sh shell will result in scripts that are portable to the greatest number of systems. Writing to
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Getting Help
The bash shell provides a help command that will give information about all its built-in commands. Example 3-14 shows how to get information about the alias command.
Example 3-14. Getting help about alias
$ help alias
alias: alias [-p] [name[=value] ... ]
  'alias' with no arguments or with the -p option prints the list
  of aliases in the form alias NAME=VALUE on standard output.
  Otherwise, an alias is defined for each NAME whose VALUE is given.
  A trailing space in VALUE causes the next word to be checked for
  alias substitution when the alias is expanded. Alias returns
  true unless a NAME is given for which no alias has been defined.
		
If you want to get a list of commands that help can help you with, use the command by itself.
To get help about any other command in the Unix shell, use man, the all-purpose, Swiss Army Knife for getting information for most commands on the system in a form known as a manpage. For example, to see the manpage for man, type the following:
  $ man man
		          
When you execute this command you'll see the output shown in Figure 3-4. Notice that there's a colon (:) at the bottom of the window with the cursor next to it. This indicates that there's more content to be seen in the manpage. At the prompt you can:
  • Hit the Spacebar or the F key to page down
  • Hit the B key to go back a page
  • Type a / (slash) followed by a word to search forward for that word
  • Hit the Q key to quit
Figure 3-4: The man command
When you quit out of viewing a manpage, the information goes away and you are left with the same contents in your window as you had before you executed the
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Editing Text Files
While editing text files in the GUI is straightforward using the built-in TextEdit (/Applications), editing files using the command-line tools hasn't always been a walk in the park unless you are comfortable with old-school text editors like vi or Emacs. But thanks to the open command, several of the command-line editing tasks that you need to perform can be done in the GUI. For example, to open up your bash history file in TextEdit, use the following command:
  $ open ~/.bash_history
		          
For most text file editing, however, TextEdit is a little underpowered. Also, it can't be used as the command-line editor program (via the EDITOR environment variable), and it can't be used to edit files while you are logged in remotely.
If you do need to edit text files on the command line—say you are logged in remotely or need to edit a file owned by root—the easiest text editor to use on the system is pico. To use pico to edit a file, use the following command:
  $ pico filename
				              
			            
When you use pico, you'll see the user interface shown in Figure 3-5. In place of a toolbar, there is a series of commands at the bottom of the screen. The caret (^) character prefixing all those lines stands for the Control key on your keyboard. Simply press the Control key plus the letter of the command to perform some action. For example, to save a file, which pico refers to as "WriteOut," hit Control-O. To open, or "Read" a file, hit Control-R. And to exit, hit Control-X.
Aside from the Control key combinations, pico is fairly straightforward to use. Arrow keys move the cursor up and down, and whatever you type is inserted wherever the cursor is located at.
Figure 3-5: The pico text editor
By default, pico is not set as the default command-line editor on the system. This means when a command-line tool, such as
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Further Explorations
This chapter's aim was to give you enough knowledge of how the shell works to successfully execute the commands that I'll be covering through the rest of the book. However, this chapter has just scratched the surface of what is possible with the shell.
The following books are recommended to learn more about the subjects in this chapter:
  • Learning the bash Shell, 2nd Edition, by Cameron Newham, et al. (O'Reilly & Associates, Inc., 1998)
  • Learning the vi Editor, 6th Edition, by Linda Lamb, et al. (O'Reilly & Associates, 1998)
  • Learning GNU Emacs, 2nd Edition, by Debra Cameron, et al. (O'Reilly & Associates, 1996)
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Chapter 4: System Startup and Login
Content preview·