Mac OS X Command Line 101
by Richard Burton
Introducing The Command Line
April 26th, 2002
"The truth is rarely pure, and never simple."
- Oscar Wilde, The Importance of Being Earnest
With the release of OS X, the Macintosh has a command line for the first time since it was introduced in 1984. Admittedly, Apple has done a great job with OS X, and the vast majority of users can go a long time without having to ever touch the terminal. In fact, many of the longtime Mac faithful don't care to learn it at all, but there are growing numbers of people who would like to know how to use it. Mac OS X Command Line 101 is going to try and help those people do just that.
While Apple has done a tremendous job with OS X, no operating system is perfect. Sometimes a problem cannot be fixed using one of the standard GUI tools, or sometimes one of those GUI tools doesn't do quite what you want. Sometimes, there are even times when using the command line is just easier.
Luckily, OS X is based on Unix. Unix was not market-driven; it was written by geeks for geeks in the real ivory tower of the past, Bell Labs. To understand what this means, consider what Larry Wall, creator of the Perl programming language, calls the three great virtues of programmers: laziness, impatience, and hubris. Laziness means you don't want to spend a lot of time and effort doing something simple, or doing it repeatedly. Impatience means you want to have the necessary tools for a particular task at your fingertips. Hubris means you want to know that the tool will just work without having to worry about it. Of course, getting to that point requires a lot of hard work, time, and testing. Fortunately, all of those attributes have gone into Unix already.
Granted, the Unix command line can occasionally look as if an inebriated cat walked over the keyboard; for first-timers, this can be a bit daunting. However, we longtime Mac loyalists are used to dealing with daunts; otherwise, we wouldn't have stuck with Apple through thick and thin, would we? While the Unix command line can have a steep learning curve, at first, part of that is because you can do so much with it.
The command line takes a toolbox approach. A tool does one job and does it well. Just as you don't use a saw to pound a nail into the wall, you should chose the right command line tool for the right job.
Unix uses shells as a command interpreter; you type the commands, the system runs them, and the shell acts as the go-between. The first shell was the Bourne Shell, created by Stephen Bourne in 1979. Later, the C shell (csh) was developed at Berkeley; it was so called because its syntax is similar to the C programming language. To encourage laziness, impatience, and hubris, an enhanced version, tcsh, was released. This is the shell that OS X uses when you start Terminal.app.
So what sets the Unix command-line apart?
Admittedly, all of this is a lot to take in at first, but you have to walk before you run, and crawl before you walk. I will start with the basics and move slowly. If you have any questions, be sure to send them to me at email@example.com. If I like it well enough, I may use it in a future column.
- The basic philosophy is "Simple things should be simple, complex things should be less simple, and practically anything should be possible."
- The Unix toolbox. While any command-line will give you tools, the commands available in Unix are fantastic in quantity and quality. If you want to do something on the command line, someone else probably has wanted to do it as well and written a command for it. What's more, Unix allows you to string commands together so you can build complex tools out of simple ones. You'll particularly appreciate them if you've ever had to use DOS or worked on a mainframe.
- Documentation. The online help files, called "man pages," are very good; and many companies provide books to help as well.
- Flexible commands. Most commands have options that will allow you to modify their behavior if the default is not exactly what you need. And even if that isn't quite what you had in mind, you can combine them to suit your purpose.
- I/O Redirection. If you want to write the output to a file, or read parameters from a file, or string commands together so that you don't have to write to/read from files, Unix shells provide a very simple way to do this.
- Multiple processes. It may seem to be hard to believe now, but back when Unix was created, this was a novel concept. We may take it for granted, but one of the beauties of Unix is allowing you to have many processes running at once. What's more you can have many on the same command line, which puts a lot of power at your fingertips.
- Job control. This lets you send one task to run in the background while you do other work; you can also pull a background job to the foreground. You can start and stop jobs. And you can remove them from the system, nicely or with extreme prejudice.
- Protected memory. If one process goes rogue, it won't interfere with the memory space of another process. Anyone who has been forced to use ... other desktops ... will appreciate this.
- 'Quotes', "quotes", `quotes`. Anyone who is given to quotation will tell you, from long experience, that you must quote in the correct context. Unix gives you three contexts, each one being appropriate in different situations.
- Regular expressions. While not unique to Unix, the OS has a rich set which give you great flexibility in searching and manipulating text. They are largely, though not entirely, consistent between commands.
- A file is a file is a file. Some systems, which shall go nameless, pack the bits differently if the file is binary or if it is ASCII. Unix makes no such distinction; even a directory is a file. The beauty of this is that you can, say, run a Perl script to make quick-and-dirty edits on a Word Document. (NOTE: That is not for the faint of heart, but the system won't stop you from trying.)
And most of all, remember to have fun.
You are encouraged to send Richard your comments, or to post them below.
Most Recent Mac OS X Command Line 101 Columns
Command Line History & Editing Your Commands
Pico: An Easy To Use Command Line Editor
Understanding The "grep" Command In Mac OS X
Command Line History & Editing Your Commands
Mac OS X Command Line 101 Archives
Back to The Mac Observer For More Mac News!
Richard Burton is a longtime Unix programmer and a handsome brute. He spends his spare time yelling at the television during Colts and Pacers games, writing politically incorrect short stories, and trying to shoot the neighbor's cat (not really) nesting in his garage. He can be seen running roughshod over the TMO forums under the alias tbone1.