This article was written in late 2002. Since then, more than
a few things have changed. Be sure to see the Related Articles also.
For one thing, Apple apparently saw the light and stopped
using tcsh as their default shell, so if you've bought a newer
Mac, your Terminal will use Bash automatically.
They've also moved to Intel chips. I bought a
MacBook Pro,
and sold the iBook to someone on eBay. The use of Intel chips
allows virtualization of x86 operating systems through products
like Parallels
Workstation. That lets me run Linux and Windows as guest OSes. You
can do that with the Motorola chips too, but it requires emulation, which makes it much slower.
There are some disquieting aspects to the Intel change. As
I write this, Apple is yet to release kernel source for the
Intel version (Mac OS X uses a Darwin core that has been open source until now). While they have yet to say that they are NOT
going to release it, the suspicion is that they are holding back
from fear of clones on ordinary Intel hardware. That could
be very dangerous to Apple's sales, but the threat may not be
as great as it might seem: Apple controls the hardware very
tightly and their OS code can be strongly slanted toward
Apple designs. There are also large parts of Mac OS X that cannot
be legally copied. It might even be good for Apple if there were
Intel clones running an inferior rake-off based on Darwin code.
There have been many other changes too, including more work in
the way daemons are started and controlled.
And we can't forget all the patches and bug fixes. Particularly
we need to remember that although we as Mac users have less to fear
from viri and malware, we are not immune, and as Macs become more
popular the danger increases. We'll likely never reach the
level that Windows has (and Windows itself should be less
vulnerable with
Vista), but complacent over confidence is dangerous.
Macs seem to be becoming more popular with the tech crowd. I have noticed more than a few folks using Mac laptops at trade shows and technical seminars. Now and then I ride the train to Boston and I've sat next to people using Macs more than once.
The presence of Unix underneath is certainly attractive for folks who want it. I think in some ways it's more interesting to the older Unix types.. the pace of change in Linux is sometimes too much for us, and the cavalier changes to commands can be upsetting. I like Linux, but the BSD base of Mac OS X is like comfortable old shoes.
This was the first Mac OS X article I wrote, but you'll find a good number of other Mac related articles here now.
By the way, if you are completely unfamilar with Unix command line interfaces, you can get a very complete and basic introduction from Take Control of the Mac Command Line with Terminal. That's an inexpensive PDF book that starts by assuming no knowledge whatsoever. It explains everything you need to know to make use of OS X Terminal.
Many Mac OS X users won't have any need to use the Unix shell that underlies their graphical interface. Some will likely disdain the very idea, but for those adventurous enough to try it, a whole new world awaits.
The shell is accessed through the Terminal program, which you will find under Applications->Utilities. Fire it up and you'll be presented with this:

The obsessively observant will notice that this shell is a "tcsh" session, which means you would be using tcsh as your shell. The very first thing I would do is go to Terminal Preferences and change it to use /bin/bash instead (current Mac OS X defaults to /bin/bash so this is unnecessary):

There's a very good reason for that. The tcsh shell is actually a pretty good shell, but its ancestry is bad. It comes from the csh, which is a simply horrible scripting shell (see http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ if you are more than mildly interested in why I say that). The history behind this is that csh was designed not as a scripting language, but as something that was easy for users. The "other" shell (/bin/sh) had good scripting and redirection power, but wasn't particularly friendly to users.
Over time, other shells came along that made the unfriendly /bin/sh more user friendly. The two that gained the most following are ksh and bash. Simple scripts in either shell will work in /bin/sh, and all but the most complicated ksh or bash scripts will work in either shell. There's no compatability with tcsh though.
Here's the thing: Mac OS X has a beautiful graphical user interface. There is no reason for you to be using the shell at all EXCEPT for its power for scripting and accessing the underpinnings of Unix. Most GOOD shell scripts you come across on the web or magazines will be sh, bash or ksh. So why not start out learning the kind of things that will really be useful? If bash were any more difficult or less friendly than tcsh, using that might make sense, but bash is every bit as easy and friendly, and some would say it is even more friendly.
Understand that I'm not saying that someone who is already comfortable with csh or tcsh should dump it and use bash. Those people should continue to use what they are comfortable with. This is directed at those of you who have no previous experience: don't waste your time with tcsh.
You'll also want to do:
chsh -s /bin/bash
and unfortunately there's even more to do to get rid of tcsh entirely. Following the directions I gave here leaves you running bash, but it has SHELL still pointing at /bin/tcsh. To fix that, you need to explicitly set SHELL in your .bashrc file. What you want is a .bash_profile like this:
. ~/.bashrc
ENV=$HOME/.bashrc
export ENV
and .bashrc like this:
PATH=$PATH:$HOME/bin:/usr/local/bin
alias lc=ls
alias l="ls -l"
SHELL=/bin/bash
Of course the PATH and aliases are my preferences.
That's the way I did it, but there is another way if you are an administrator. In Applications/Utilities you will find NetInfo Manager. Fire that up, and if the domain "/" isn't already open, choose "Open" and once "/" is open, click the little lock at the bottom left of the panel. You'll be asked for your password, and then are allowed to make changes. Drill into Users, find your account, and then double click "shell" and change it to /bin/bash.
Netinfo is gone as of Leopard (October 2007). Good riddance.
Obviously this isn't traditional Unix. If Netinfo is what it really takes to change my shell, then /etc/passwd isn't being used for that. If I examine the /etc/passwd file I can see my account there, but that's NOT the account used for logging in at the console. Even more strange is that I can "su" to accounts that are NOT in /etc/passwd (but are console login accounts). There's more going on here than meets the eye: some of the function of traditional Unix files is handled ny Netinfo instead.
Mac OS X has made some changes that old Unix folk may find a little disconcerting. First, logging out doesn't close the shell window (you can change that in Window Settings). More unusual (for Unix folk) is that case doesn't always matter. I can:
mkdir appl
cd Appl
and I'll be in "appl". That's not true for wild cards though: I cannot
mkdir appl
cd Ap*
This is very confusing for old Unix hands. After making "appl", I cannnot "mkdir Appl". This can also cause problems with files brought from other Unix systems - although it is a little unusual, it is not unheard of to have two very different files whose names only differ in capitalization. Keep that in mind.
You are also allowed to do such awful things as
VI MYFILE
Of course you had better understand that Vi isn't going to put up with this "case doesn't matter" nonsense.
The integration of Mac files to Unix is not entirely seamless. One difference is that Mac considers a text file to have only Carriage Returns (CR) as line endings. Windows and Dos use Carriage Return and Linefeed (CRLF) and Unix uses only Line Feeds (LF). So if you save a file as text from most Mac applications, it gets saved with CR line endings. Unix programs (yes, even Mac OS X Unix programs) expect LF endings. If you will be using Unix text editors (and you will), you will need to convert the files. That's not hard, but we'll get to it later. Keep this in mind if you are learning shell scripting and thought that it would be nice to use, say, AppleWorks to create the scripts. That just won't work.
Finally, Unix has no concept of the Data and Resource forks that Mac applications use. So while ordinary Unix usage would be to copy a file like this:
cp file newfile
that won't work with applications. Mac OS X provides Unix "Cpmac" and "Mvmac" commands to remedy that.
Yes, it is. You can pick up a lot of knowledge just be reading about other Unix systems. Mac OS X is based on BSD Unix, but a lot of articles about Linux or general Unix will be applicable. As I mentioned earlier, most shell scripts will be bash compatible, and of course any book on Bash will be very useful.
You can even discard all the graphical niceties if you like. Yes, there is a simple white on black vt100 screen in there if you want it. One way to get it is to either enable the root account (NetInfo Manager, Security, Enable Root User). That creates an "Other" login box that you can choose and either type "root" into or ">console". There's no password for >console, but then you have to log in again (as yourself or root) at the vt100 screen. Logout to return to the normal graphical login screen.
Another way to get there is do do a "sudo shutdown now", which looks like it's bringing you to single user mode. You can also get to that by holding the Apple Key and "S" during startup. The only way out of there is reboot: this is BSD Unix, not System V with init states.
There are several things that your machine should do periodically. If you left your machine up and running (and didn't let it go to sleep), it would do these things itself, but they are scheduled for odd times of day by default. You can have it do this work yourself by running:
sudo /etc/daily
sudo /etc/weekly
sudo /etc/monthly
("sudo" will ask for a password - that's YOUR password it wants. See Sudo for more information)
Run these daily, weekly, and monthly just as their names imply. In another article I'll cover what they do and show you more ways to have them run automatically, but this will work for now.
I'll close with the "vi" command to convert Mac files to Unix (I have more on "vi" at Vi Primer). "vi" is another of those Unixy things that most folks hate, but die hard Unix folks couldn't live without. Understand ahead of time that vi is not supposed to be friendly. It is supposed to be fast and powerful. The comparison is your Ford SUV vs. a competition rail dragster. You probably wouldn't want to use the dragster to go to the supermarket, but if you had to cover a quarter mile as fast as possible that would be your choice. Likewise, vi probably will not be your favorite word processor (though it is mine). But vi and its cousins will be indispensable when you need to make complex, automatic edits to text files. Anyway, here's what you do to "fix" Mac text files in vi:
Hit ":". The cursor drops to the bottom of the screen. Type "1,$s/" and then press CTRL-V followed by CTRL-M. When you press CTRL-V nothing appears to happen, but the CTRL-M shows up as "^M". Continue with "/" and then CTRL-V again. Hit RETURN (which will show up as ^M and you could do that too - I just like it this way) and finally "/g". On your screen the whole thing looks like:
:1,$s/^M/^M/g
What does that mean? It means "Starting at line 1 and stopping at the end of the file (1,$), substitute (s) any CTRL-M (/^M/) with Unix CTRL-M (^M/) and do it for the entire line rather than just the first CTRL-M you find (g) (On most other Unixes I'd just do s/^M//g ; I don't know why Mac OS X didn't let me do that). It is a little strange that you replace ^M with ^M but get something entirely different, but that's a subject for another day. The morbidly curious can start by typing "man stty" if they need to know now.
Several people have written to point out that %s is equivalent to 1,$. That's true, but I never use it because I just prefer being specific.
Now ":wq" to write it out and the file now has Unix style line endings.
Of course you don't need vi if you would rather put this in another file:
cat file1 | tr "\\r" "\\n" > file2
will also convert line endings.
Want more? You can get a very complete and basic introduction to the OS X command line from Take Control of the Mac Command Line with Terminal, an inexpensive PDF book that starts by assuming no knowledge whatsoever. It explains everything you need to know to make more use of OS X Terminal.
I hope these tips will get you started using the shell in Mac OS X terminal.
Small mac support site for iDVD and OS X Server.
Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them.
I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.
Have you tried Searching this site?
Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates
This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.

---December 19, 2004
Great Quickstart. This article is a great timesaver if you are migrating from Linux.
-- Stywiz
Sun Mar 20 23:56:40 2005: Subject: About case sensitive filenames... anonymous
Great tutorial - and thats comming from a Unix veteran
However, it is important ro note that durring install of OS X, you have the option to choose UFS (Unix File system) instead of HFS (Mac default).
The default HFS+ file system is a journaling version of the origonal HFS that Apple has used for years. One thing however that Unix File System has over apples is that it IS aware of letter case.
For instance, on my iMac, "Music" and "music" are NOT the same folders.
If you are comming from a *nix system to try out OS X, you may wish to opt for using UFS instead.
-Dean (dean@whitestealth.net)
Mon Mar 21 00:11:27 2005: Subject: TonyLawrence
Good points, thanks. I frankly never thought about it when I installed - I might next time, though by now I'm pretty used to it..
Wed Oct 5 13:42:58 2005: Subject: UFS on Mac anonymous
causes a host of other problems. Although Mac OSX can use it, it doesn't like it on the boot disk and you'll spend a good bit of time trying to fix those things.
Sat May 27 03:42:18 2006: Subject: Fixing Line Endings anonymous
FYI: rather than using the full addressing method to specify the whole file, vi has a shortcut for "1,$s/", and that is the "%" character.
So it would be more efficient to use "%s/"
Being a very old hand with unix (going back to SVR3), and linux , I've just got my new MacBook Pro and I love it. Your info was very informative.
For a long time I've always thought if someone could put a friendly face on top of the power of Unix, they would clean up. It looks like Apple is on the way there.
Sat May 27 10:22:35 2006: Subject: % for 1,$ TonyLawrence
Yeah, I use %, but when doing articles I like to show the long form because it teaches that you can limit the scope: 5,9s/foo/baa/ and so on.
Generally speaking I avoid "clever" and show "vanilla". Sometimes I think that's and idea more people should follow: http://aplawrence.com/Security/slow_down.html
Mon Mar 31 15:47:14 2008: Subject: ( This comment got tagged as spam by my spam filter. Alway anonymous
apl,
thanx for the Using the shell (Terminal) in Mac OS X info,
nonetheless for me using vi, i am getting the command not recognized error?
how could this be, what am i missing ?
thanx
Mon Mar 31 17:43:18 2008: Subject: TonyLawrence
You are going to have to be more specific. Show us exactly what happened (cut and paste from Terminal is best).
Macworld Mac Basics Superguide, Leopard Edition $12.95
Macworld Mac Gems Superguide $12.95
Macworld Mac OS X Hints Superguide, Leopard Edition $12.95
Mon Mar 31 18:37:56 2008: Subject: TonyLawrence
His problem was simply that he was doing ":dd" in vi to delete a line. It's just "dd", not ":dd".
Mon May 4 01:33:18 2009: Subject: PASSWORD anonymous
hey is it an issue if i have entered my password incorrectly in terminal;
how do i reset it or do i do the newuser code?
Mon May 4 10:14:47 2009: Subject: TonyLawrence
I have no idea what you are asking. Where and why did you enter your password??
Wed May 6 08:58:16 2009: Subject: anonymous
i was installing an activation patch for a program
Wed May 6 11:29:22 2009: Subject: TonyLawrence
Like pulling teeth :--)
Go read http://www.catb.org/~esr/faqs/smart-questions.html
Don't miss responses! Subscribe to Comments by RSS or by Email
Click here to add your comments
If you want a picture to show with your comment, go get a Gravatar