3.2 AliasesPerhaps 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. [2] You define an alias by entering (or adding to your .profile ) a line with the following form:
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, so that whenever you type new , the Korn shell substitutes original internally. 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, but the classic example is: alias search=grep grep , the UNIX file-searching utility, was named as an acronym for something like "Generalized Regular Expression Parser." [3] This acronym may mean something to a computer scientist, but 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:
$ 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 reads commands in from 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. You might want the file .logout to "clean up" your history files, as we discussed in the last chapter. Recall that we created history files with the filename .hist$$ , which guarantees a unique name for every shell. To remove these files when the shells exit, just put this line in your .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' Notice the quotes around the full cd command; these are necessary if the string being aliased consists of more than one word. [4]
As another example, a useful option to the ls
command is -F
:
it puts a slash (/
) after directory files and an asterisk ( 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
alias printall='pr * | lpr' 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 will work as expected (typing ls produces a long list with permissions, sizes, owners, etc.), while in more pathological situations such as: alias listfile=ls alias ls=listfile the alias listfile is ignored. 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: cm: not found . 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 blank, 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 blank, you need to surround it with quotes. Here is how you would use this capability to allow aliases for directory names, at least for use with the cd command. Just define: 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. 3.2.1 Tracked AliasesAnother rather obscure feature of the alias facility is the tracked alias, which can shorten the time it takes the shell to invoke commands. If you specify this option (as shown under "Options" below), then for all subsequent alias definitions, the shell will internally substitute the full pathname of each command for which an alias is defined. You can also define individual tracked aliases with the option -t to the alias command, and you can list all tracked aliases by typing alias -t by itself. As you will see later in this chapter, a tracked alias cuts down the number of steps the shell has to take to find the command when you want to run it. More important, however, are its implications for system security; see Chapter 10, Korn Shell Administration . For example, assume that you have defined the alias em for the emacs editor, which is kept in the executable file /usr/local/bin/emacs . If you specify that you want aliases tracked, then the first time you type em myfile , the shell will substitute the full pathname, i.e., as if you had defined the alias as: alias em=/usr/local/bin/emacs You'll see how this can save time when you read about the PATH environment variable later on. 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 will print the alias' value or alias name 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. The command unalias name removes any alias definition for its argument. 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. |
|