A Mac User’s Introduction to the UNIX Command Line

I’ve always loved teaching (for many reasons, some of which are too arrogant to get away with mentioning) and today my supervisor asked me if we could spend a little while talking about the command line and Mac OS X’s UNIX underbelly. Of course, I was thrilled at the prospect (see earlier statement on my love of teaching) and so I offered up a couple of resources. Since I think they’d be helpful for a lot of Mac users who’ve gotten bit by the UNIX bug (no pun intended), I expanded my list into a mini-guide.

Shells and Terminals: What’s the difference?

The very first thing anyone thinks about when they think UNIX is the command line interface (or CLI, for short). The funny thing about the CLI is that it’s actually a program in and of itself called a shell that takes what you write on the command line and basically translates it to the computer so it will do what you tell it to do.

So the computer (or the OS) is really only accessible through this shell. This is fairly straightforward until you realize that the shell is usually only accessible through another program, commonly called a terminal, which on Mac OS X is called Terminal.app. Therefor the terminal is really a window to a shell which is another window to the OS, which then talks to the computer for you. So when people say they want to learn to use the command line, you can understand the confusion that might cause. What shell do they want to use? And on top of that, which program are they intending to use to access their shell? Even on the Mac, there are at least two built-in terminals you can use (Terminal.app and XTerm) and maybe half a dozen shells.

As far as terminals go, Terminal.app is the most accessible (and you can find it in your Utilities folder in your Applications folder on your startup disk). My favorite resource for getting started with Terminal was MacDevCenter‘s excellent article, Learning the Mac OS X Terminal, the title of which makes a lot more sense after understanding the previous two paragraphs. That article will show off some of Terminal’s impressive features that make a GUI environment complement the command line environment nicely.

The Command Line: Speak and it will be so

The command line itself is, of course, yet another subject entirely. Thanks to its spartan appearance, I think the best way to learn it is to just use it. If you’re feeling brave and have a spare computer lying around, take the leap and install a Linux or a BSD distro, but run it all from the command line (which is simply a matter of changing the active runlevel on most UNIX-like OS distros). This will effectively boot you into a shell, with no GUI coating. For Mac users who want to stay closer to home, you can do the same thing by logging into Mac OS X as the >console user (with no password). This strips Mac OS X of its Aqua interface and drops you into a login shell.

The important thing to remember is that, while in a shell, you can do everything you can do in a GUI mode. You can browse the web, edit and view documents, and even read and compose email. Of course, there won’t be any pictures, but the point remains that these things can be done. A week of running your computer in a shell and you’ll feel more comfortable than ever at the command line.

But while you’re there, you’ll want to acquaint yourself with several commands you can use to get help. The most important is the man command, which will let you browse the documentation for any command you want to use. You can even browse the documentation on the man command itself by typing man man (and I strongly recommend that you do that).

Another command you’ll want to get familiar with is apropos. If you type man apropos, you’ll get a very description of what that command does. (I won’t spoil the treat for you.) A similar command to apropos is whatis. All these commands are just programs like any other, absolutely identical to the ones you’re already using with the sole exception that instead of outputting pixels to a screen, they output characters to a shell.

On the web, one of my oft-referenced resources for learning the basics of a shell environment (and thus the command line) is TuxFiles. It’s geared towards Linux users, but the things you can learn there apply to all UNIX-like operating systems. Another one is LinuxCommand. Check them both out.

Inside the Shells

By now you’re hopefully running your computer with a terminal window constantly open in the background, or at least you’re excited about trying it. So what’s next? Learn a new shell! By default, most OS’s will use bash, an abbreviation for the Bourne Again Shell, which itself is based on the Bourne Shell (get it, get it?), written by Steve Bourne.

Different shells all do the same thing, which is enable you to speak with (by typing to) your computer and have it do your bidding, but they understand different dialects so you need to speak to them in slightly different ways. There are dozens of shells you can use, and which one you ultimately choose depends on the features and syntax of each. I spent a good deal of time reading about them all on Wikipedia before I decided to scrap the whole ordeal and stick with Mac OS X’s default bash shell. However, I learned a lot along the way, and that was really the point.

Speaking Command Line is like learning a new language

Ultimately however, it’s not about the shell or terminal you use, but about the things you can do. This, unfortunately, has to come with time. It’s exactly like learning a language. Every command you learn is akin to learning and using a new verb. The more you use a new language to communicate, the better you’ll become at speaking it. The same is true of the command line.

Keep your terminal program open 24/7/365, and whenever you’re doing something, ask yourself if you could do the same thing on the command line. If you’re not sure, Google it, and if the answer is yes, do it that way the next time. This is especially useful when what you’re doing is more than one thing. It takes a dozen or so clicks to open a file in TextEdit, copy it to the clipboard, past it to a new document, then add some text to the end of the new document, and finally save it again, but all of that can be done by typing a single line in a terminal. (Hint: use your shell’s piping and I/O redirection features.)

The more you immerse yourself, the better you’ll become. If you’re really dedicated, I’ve heard nothing but great things about the book Learning Unix for Mac OS X Tiger, which describes itself as a guided tour of Unix designed for a Mac user. Ah, just what the curiosity ordered.

One reply on “A Mac User’s Introduction to the UNIX Command Line”

Comments are closed.