Book HomeBook TitleSearch this book

3.2. Aliases

Perhaps the easiest and most popular type of customization is the alias, which is a synonym for a command or command string. This is one of several Korn shell features that were appropriated from the C shell.[35] You define an alias by entering (or adding to your .profile) a line with the following form:

[35] C shell users should note that the Korn shell's alias feature does not support arguments in alias expansions, as C shell aliases do.

alias new=original

(Notice that there are no spaces on either side of the equal sign (=); this is required syntax.) The alias command defines new to be an alias for original; whenever you type new, the Korn shell substitutes original internally. (You cannot use any of the shell's special characters, such as *, $, =, and so on, in alias names.)

There are a few basic ways to use an alias. The first, and simplest, is as a more mnemonic name for an existing command. Many commonly used Unix commands have names that are poor mnemonics and therefore are excellent candidates for aliasing; the classic example is:

alias search=grep

grep, the Unix file-searching utility, derives its name from the command "g/re/p" in the original ed text editor, which does essentially the same thing as grep. (The regular expression matching code was carved out of ed to make a separate program.)[36] This acronym may mean something to a computer scientist but probably not to the office administrator who has to find Fred in a list of phone numbers. If you have to find Fred, and you have the word search defined as an alias for grep, you can type:

[36] Thanks to Dennis Ritchie and Brian Kernighan of Bell Labs for verifying this for me. ADR.

search Fred phonelist

Another popular alias eschews exit in favor of a more widely used command for ending a login session:

alias logout=exit

If you are a C shell user, you may be used to having a .logout file of commands that the shell executes just before you log out. The Korn shell doesn't have this feature as such, but you can mimic it quite easily using an alias:

alias logout='. ~/.ksh_logout; exit'

This executes the commands in the file .ksh_logout in your home directory and then logs you out. The semicolon acts as a statement separator, allowing you to have more than one command on the same line.

Notice the quotes around the full value of the alias; these are necessary if the string being aliased consists of more than one word.[37]

[37] This contrasts with C shell aliases, in which the quotes aren't required.

You might want the file .ksh_logout to "clean up" your history files, as we discussed in the last chapter. Recall that we created history files with names like .hist.42, which guarantees a unique name for every serial line or window. To remove these files when the shells exit, just put this line in your .ksh_logout file:

rm ~/.hist.*

Some people who aren't particularly good typists like to use aliases for typographical errors they make often. For example:

alias emcas=emacs
alias mali=mail
alias gerp=grep

This can be handy, but we feel you're probably better off suffering with the error message and getting the correct spelling under your fingers. Another common way to use an alias is as a shorthand for a longer command string. For example, you may have a directory to which you need to go often. It's buried deeply in your directory hierarchy, so you want to set up an alias that will allow you to cd there without typing (or even remembering) the entire pathname:

alias cdcm='cd ~/work/projects/devtools/windows/confman'

As before, the quotes around the full cd command are needed, because the string being aliased has more than one word.

As another example, a useful option to the ls command is -F: it puts a slash (/) after directory files and an asterisk (*) after executable files. (Depending on your system, it may append other characters after other kinds of files as well.) Since typing a dash followed by a capital letter is inconvenient, many people like to define an alias like this:

alias lf='ls -F'

A few things about aliases are important to remember. First, the Korn shell makes a textual substitution of the alias for that which it is aliasing; it may help to imagine ksh passing your command through a text editor or word processor and issuing a "change" or "substitute" command before interpreting and executing it.

This, in turn, means that any special characters (such as wildcards like * and ?) that result when the alias is expanded are interpreted properly by the shell. This leads to an important corollary: wildcards and other special characters cannot be used in the names of aliases, i.e., on the left side of the equal sign. For example, to make it easier to print all of the files in your directory, you could define the alias:

alias printall='pr * | lp'

Second, keep in mind that aliases are recursive, which means that it is possible to alias an alias. A legitimate objection to the previous example is that the alias, while mnemonic, is too long and doesn't save enough typing. If we want to keep this alias but add a shorter abbreviation, we could define:

alias pa=printall

Recursive aliasing makes it possible to set up an "infinite loop" of definitions, wherein an alias ends up (perhaps after several lookups) being defined as itself. For example, the command:

alias ls='ls -l'

sets up a possible infinite loop. Luckily, the shell has a mechanism to guard against such dangers. The above command works as expected (typing ls produces a long list with permissions, sizes, owners, etc.). Even more pathological situations work, such as these:

alias listfile=ls
alias ls=listfile

If you type listfile, ls runs.

Aliases can only be used for the beginning of a command string -- albeit with certain exceptions. In the cd example above, you might want to define an alias for the directory name alone, not for the entire command. But if you define:

alias cm=work/projects/devtools/windows/confman

and then type cd cm, the Korn shell will probably print a message like ksh: cd: cm: [No such file or directory].

An obscure, rather ugly feature of the Korn shell's alias facility -- one not present in the analogous C shell feature -- provides a way around this problem. If the value of an alias (the right side of the equal sign) ends in a space or a tab, then the Korn shell tries to do alias substitution on the next word on the command line. To make the value of an alias end in a space, you need to surround it with quotes.

This feature exists so that it is possible to have aliases for commands that themselves run other commands, such as nohup and nice. For example, nohup is aliased to 'nohup '. That way, when you type:

nohup my_favorite_alias somefile

the shell will expand my_favorite_alias just as it would when typed without the preceding nohup command. (The nohup command is described in Chapter 8.)

Here is how you would use this capability to allow aliases for directory names, at least for use with the cd command:

alias cd='cd '

This causes the Korn shell to search for an alias for the directory name argument to cd, which in the previous example would enable it to expand the alias cm correctly.

The Korn shell provides an efficiency feature called "tracked aliases." We delay discussion of these until Section 3.4.2.8. Also, a number of aliases are predefined by the shell; they are listed in Appendix B.

Finally, there are a few useful adjuncts to the basic alias command. If you type alias name without an equal sign (=) and value, the shell prints the alias's value or name: alias not found if it is undefined. If you type alias without any arguments, you get a list of all the aliases you have defined as well as several that are built-in. If you type alias -p, the shell prints all your aliases, with each one preceded by the alias keyword. This is useful for saving all your aliases in a way that allows them to be re-read by the shell at a later time. The command unalias name removes any alias definition for its argument. If you type unalias -a, the shell removes all aliases.

Aliases are very handy for creating a comfortable environment, but they are really just kid stuff compared to more advanced customization techniques like scripts and functions, which we will see in the next chapter. These give you everything aliases do plus much more, so if you become proficient at them, you may find that you don't need aliases anymore. However, aliases are ideal for novices who find Unix to be a rather forbidding place, full of terseness and devoid of good mnemonics.



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.