Departments & Programs

More

Aliases



Last revision August 9, 2004

Table of Contents:

  1. Shell interpretation
  2. Editing command lines
  3. Stuck in a Unix login session?
  4. Program execution
  5. Simple commands
  6. Unix command syntax
  7. Controlling processes
  8. Your login environment

An alias is an alternative name. The shell has an alias mechanism that allows you to customize or abbreviate names of commands. It is more powerful than simple synonyms, in that you can alias a new name to a command template with some arguments or options already specified, and then have the shell insert the arguments you type on the command line into the proper place in the template.

A simple alias could be:

alias ls ls -F

This alias will always show the file type indicator (-F option) when you give an ls command.

A more complicated example of a template is:

alias cd 'cd \!* ; pwd'

Here, when you type cd newdir, the alias command will substitute cd newdir; pwd, which will always execute a pwd to show the current working directory after you do a cd.

Aliases should go into your .login file, if just intended for interactive use, or into your .cshrc file, if you want them to be available to shell scripts that you may run. Having too many slows down your responses as the shell compares every command you type to its list of current aliases.

Warning: if you create your own alias commands, always put single quotes around the replacement text portion if you have any shell metacharacters in there. Otherwise the shell will misinterpret the alias command and either create the wrong alias, or in some cases, even cause your logins to hang.

For example, suppose you want to create an alias to send the output of some program that you often run, such as w, through the more filter to control the screen display. If you create this alias command in your .login, you will not get the result you expect:

alias wm w | more

Remember that the shell must first parse this line before it can execute it to create the alias. When it parses the line, it will notice the pipe symbol (|) and compute, "he wants to run two separate commands in a pipeline. It will then start the alias command with only the two arguments wm w as one process, and try to pipe the output of that process to the more command, running as a second process. The wrong alias is created: wm simply becomes an alias for w.

On some Unix systems, you also get a peculiar side effect from this mistaken alias. The alias command in the first part of the pipeline creates no output, so the more command may just wait indefinitely for some input to appear, causing your terminal to appear to freeze.

The solution to get the alias you want is to put the replacement text in quotes so the shell will not interpret the pipe symbol at this stage, but rather make it part of the alias to be interpreted later when the alias is actually used as a command, for example:

alias wm 'w | more'

<--Previous Overview Next-->

Comments or Questions?