Using UNIX aliases to save keystrokes

Feb 01, '01 06:35:08PM

Contributed by: robg

If you're a complete beginner to UNIX (as I still consider myself), you can use something called aliases to save yourself a lot of typing at the command line.

In the Mac OS, an alias is simply a pointer to another file. In the UNIX world, an alias is similar in concept, except that it's a command that points at another command. There are a few pre-defined aliases in the tcsh shell (the program that runs when you open a terminal). You can see these by typing

alias
at the command line. One of the more useful pre-defined aliases is ll which replaces ls -lag for complete file listings.

Although the pre-defined aliases are useful, the real power is in creating your own aliases for your often-used commands. If you're new to UNIX and you'd like to learn about aliases and how to use them, read the rest of this article.

You can easily define your own aliases in UNIX. Here's an example. When I connect to my home machine from work, I use the secure shell (SSH) built into OS X. However, to connect to the machine, I have to enter the following command (phony IP address listed, of course!):

ssh -l robg -p 7022 192.168.1.1
This is obviously a bit of a pain. However, I can easily create an alias to do all the hard stuff:
alias hssh 'ssh -l robg -p 7022 192.168.1.1'
Within that terminal session, whenever I type hssh, I'll connect to my home machine. To remove the alias, you use (logically enough) the unalias command:
unalias hssh
However, if I close the window and open a new terminal session, the alias will be gone. Aliases entered at the command line are effective for that session only.

To make the aliases stick, you need to save them in a file that the tcsh shell reads each time you open a terminal session. This file is called .tcshrc, and it lives at the top level of your home directory (/Users/username). To create it, simply open it in your favorite editor:
vi /Users/robg/.tcshrc
Enter any aliases you'd like to use in the form of
alias aliasname 'command to be aliased'
You only need to use the quotes if the command to be aliased contains spaces; separate aliases with a carriage return. So my sample .tcshrc file might look something like
alias hssh 'ssh -l robg -p 7022 192.168.1.1'   ** My home machine **
alias mssh 'ssh -l macosxhints 192.168.1.3' ** The MacOSXHints webserver **
alias wcd 'cd /Library/WebServer/Documents' ** My local webserver folder **
Obviously, don't enter the comments noted following the '**' in the examples above.

When you're done, save the file, and the next time you open a terminal window, you'll have your aliases ready for use. This is a very very brief overview of the use of aliases (and the .tcshrc file). Both can be used in much more advanced ways, and for many other purposes. I'm sure some of the more UNIX-savvy users out there can probably list a bunch of other cool things to do with aliases and the .tcshrc file, but this primer should get you started.

Comments (4)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20010201183508573