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
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.
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
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
Here is how you would use this capability to allow aliases
for directory names, at least for use with the cd
command:
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.