--> --> Aliases -->

Aliases

Aliases are a way of substituting a command for another command, a command with arguments, or for multiple commands. Here is a simple example of an alias:

alias dir ls

In this example, whenever dir is given as a command, the shell will substitute it with the ls command. Any arguments to the dir alias will be attached to the substituted command, so dir -l mr.file will turn into

ls -l mr.file.

Another example:

alias ls ls -l

Here each time you use the ls command, the shell will substitute it for ls with the long listing option -l. This alias will also effect other aliases, so the dir alias will be substituted with ls, which in turn will be substituted with ls -l.

Lets take a look at a little more complicated example:

alias mypager "echo first file is \!^; cat -n \!* | more"

In this example we have quoted the alias, to prevent the shell from seeing the ';' and trying to pipe stuff to more when we want that to be done at the time the substitution of the alias is done. Furthermore, !^ and !* are escaped so the shell does not do history substitution now. This alias will create a command called mypager which will take a list of files as an argument, tell you the name of the first file, and then display the files using cat. The "-n" option of cat displays line numbers. The output of all this is then piped to more, so that it is displayed one screen at a time.

The shell is smart enough to resolve an alias such as alias ls ls -l where the substitution to ls -l, will invoke the command ls, and not re-substitute ls -l to ls -l -l and so on. However, csh will let you do the following sequence:

alias dir ls
alias ls dir

which when either dir or ls is executed, will throw the shell into an infinite loop. Tcsh, on the other hand, will report an alias loop error, and will not execute the substitution.

How do I:
How are we doing? Comments on this page?
Office of Information Technology
Office of Information Technology Help Desk Web Site University of Maryland Web Site Office of Information Technology Web Site