You'll get your Mac news here from now on...

Help TMO Grow

Subscriber Login

Advertising Info


The Mac Observer Express Daily Newsletter


More Info

Site Navigation

Home
News
Tips
Columns & Editorials
Reviews
Reports
Archives
Search
Forums
Links
Mac Links
Software
Reports
Contact

Mac OS X Command Line 101
by Richard Burton


More Basics For The Shell
Part IX of this series...
June 21st, 2002

"Que?"
"Right, let's start at the beginning. English is a non-inflected Indo-European language ..."
-
Rowan Atkinson, Blackadder II"

This series is designed to help you learn more about the Mac OS X command line. If you have any questions about what you read here, check out the earlier columns, write back in the comments below, or join us in the Hardcore X! forum. "Que?"
"Right, let's start at the beginning. English is a non-inflected Indo-European language ..."
- Rowan Atkinson, Blackadder II

So far, we have built your syntax of tcsh to the point where you can do same fairly complex things. However, before we go too deep, let's step back and add to our breadth of shell knowledge.

One thing you may have noticed so far is that the Unix shell hides a lot of detail from you, or more accurately, hides them for you. Redirecting output to a file or another process may seem trivial, but if you've ever tried this in other command-line languages, you will truly appreciate the simplicity and elegance of the Unix shell. If you tried to do this using other operating systems' command lines, like VMS, ... on second thought, don't even think about it. However, now that we've seen the machine running, let's take a look under the hood.

Recall from way back when that the ls command lists files in a directory:

    [localhost:~] dr_unix% ls
    Adam.txt   Documents  Movies     Pictures   Sites      personal   test_1.txt
    Desktop    Library    Music      Public     login      temp.html  who_list
    [localhost:~] dr_unix%
    
and that the -a option will show all files that start with a '.':
    [localhost:~] dr_unix% ls -a
    .                         .qt                       Music
    ..                        .saves-450-localhost~     Pictures
    .CFUserTextEncoding       .saves-456-localhost~     Public
    .DS_Store                 .ssh                      Sites
    .FBCIndex                 .tcsh_history             login
    .FBCLockFolder            Adam.txt                  personal
    .Trash                    Desktop                   temp.html
    .addressbook              Documents                 test_1.txt
    .addressbook.lu           Library                   who_list
    .cshrc                    Movies
    [localhost:~] dr_unix% 
    
Files that start with a '.' are known as "hidden files". Recall that two of them, '.' and '..', have already been discussed. ('.' is a shorthand for 'this directory' and '..' for 'my parent directory', if you've forgotten.) Normally, you don't notice them, nor would you care to. Hidden files are used for things like storing settings for a particular application. Thus, Unix' mail uses the file ~/.mailrc, ssh makes use of a directory called .ssh, the vi editor uses ~/.exrc (not .virc, because it is based on the older ex editor), and so on. And on and on ...

One that we will focus on is the .cshrc file. As we've said, the default shell for OS X is tcsh, an extended, more user-friendly version of the venerable old C-shell (csh). When you open a window with Terminal, the startup files .login and .cshrc in your home directory are run. Actually, if you have a .tcshrc in your home directory and you use tcsh, that is run instead of .cshrc. So when you see me talking about the .cshrc file, take that to mean "the .tcshrc file if it exists, otherwise the .cshrc file." If none of these files exist, tcsh will not complain. (And if you are using a different shell, consult your local manpage.)

Now, why would you want to have these files which are automagically run whenever you start? Simple; they can make your life easier by automating settings that you would otherwise have to spend minutes typing whenever you logged in. These include, though are not limited to:

  • Assigning values to variables, particularly environment variables. (We'll discuss these soon.)
  • Setting aliases. Would you like to be able to type 'll' instead of 'ls -alrsFt' from the command line? You can set an alias to permit this. (Again, coming soon.)
  • If you connect to another system with a different default terminal, you can alter the terminal settings so that the keystrokes are familiar. This is most commonly used to set the backspace/delete key.
  • Altering the behavior of the command line editor. tcsh comes with a simple command line editor, using the arrow keys to move through your command history. However, it is limited. If you are familiar with vi or emacs, you may wish to use their syntax to edit commands you've previously typed. (This is particularly nice if you are as inaccurate a typist as I am.)
  • Altering the default behavior of completions. (Again, a later column.)
As an example, let's look at my .cshrc file. It is fairly sparse, but it does give a flavor of what these files do:
    [localhost:~] dr_unix% more .cshrc
    setenv PATH "${PATH}:/mysql/bin"
    bindkey -v
    biff y
    alias ll 'ls -altF'
    [localhost:~] dr_unix% 
    
Don't worry yourself too much about the details, but here is what each line does. The first resets the PATH environment variable, appending the directory /mysql/bin to the current value. The next line tells the command line editor to use vi's interpretations of the various editing commands. The third says to inform me (beep) when I receive mail. The last line allows me to type 'll' instead of the longer, more error-prone 'ls -altF' from the command line and get the same information. Whenever I open a terminal window, ~/.cshrc is run, setting these values and saving me some work. You can imagine how a fussy, persnickety user might want to change all sorts of things and thus create a truly massive .cshrc file.

At this point, you may be wondering why there are two files, .login and .cshrc, which are run at startup instead of one. I've often wondered that myself, going back to when I first used csh in college. (And yes, there were computers back then.) The best explanation I got was that someone had the idea to place certain system-wide settings (as in biff y to turn on mail notification) into .login, but things specific to shell scripts would be placed in .cshrc instead. (In my current setup, running a script would mean that /mysql/bin would appear twice at the end of the PATH environment variable. This doesn't break anything, but it is sloppy. I should change that one day.) I don't know how valid this is, but it's the best explanation I ever got. Regardless, I generally put things into .cshrc instead of .login, but your mileage may vary.

As you might imagine, there is an equivalent .logout file that runs when you logout. It is not used too often; I have used it (if at all) for cleaning up directories where I like to dump messes.

For OS X, there are three system files that are also read when you log in or out. They are owned by root, the administrator account. They are /etc/csh.login, /etc/csh.cshrc, and /etc/csh.logout, and each does what you'd think.

    [localhost:~] dr_unix% ls -l /etc/*csh*
    -rw-r--r--  1 root  wheel  31 Sep  2  2001 /etc/csh.cshrc
    -rw-r--r--  1 root  wheel  34 Sep  2  2001 /etc/csh.login
    -rw-r--r--  1 root  wheel  35 Sep  2  2001 /etc/csh.logout
    [localhost:~] dr_unix% 
    
They allow a system administrator to create a repository of default settings (like a default printer name) that everyone will need to know and need not worry about administering themselves. While they are handy if you are on a network, for most of us (home users of OS X), there is little if any point in altering them; in fact, don't. You'll only be inviting trouble. I just wanted you to be aware of them.

In this column we touched on a few basics; in the following columns we will examine some of them more closely, starting the next time with variables.

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
November 22nd

Pico: An Easy To Use Command Line Editor
November 1st

Understanding The "grep" Command In Mac OS X
October 4th

Command Line History & Editing Your Commands
September 6th

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.



Today's Mac Headlines

[Podcast]Podcast - Apple Weekly Report #135: Apple Lawsuits, Banned iPhone Ad, Green MacBook Ad

We also offer Today's News On One Page!

Yesterday's News

 

[Podcast]Podcast - Mac Geek Gab #178: Batch Permission Changes, Encrypting Follow-up, Re-Enabling AirPort, and GigE speeds

We also offer Yesterday's News On One Page!

Mac Products Guide
New Arrivals
New and updated products added to the Guide.

Hot Deals
Great prices on hot selling Mac products from your favorite Macintosh resellers.

Special Offers
Promotions and offers direct from Macintosh developers and magazines.

Software
Browse the software section for over 17,000 Macintosh applications and software titles.

Hardware
Over 4,000 peripherals and accessories such as cameras, printers, scanners, keyboards, mice and more.

© All information presented on this site is copyrighted by The Mac Observer except where otherwise noted. No portion of this site may be copied without express written consent. Other sites are invited to link to any aspect of this site provided that all content is presented in its original form and is not placed within another .