|
|
When executing a command line, the Korn shell checks each word in turn against its table of known aliases. If the word is an alias name, and is not quoted in any way, and the shell is not already processing an alias of that name, then the alias name is replaced by the value of the alias. The process stops after the shell detects and substitutes one alias, unless the alias is followed immediately by a space. A similar process occurs under the C shell, except that aliases can explicitly refer to the history list (to recall a previously issued command).
It is possible to embed aliases so that an alias definition includes a command that is itself an alias. For example:
$Here, the aliased command count uses the alias dir to list (in full) the files in the current working directory; the output is piped into wc to give a line count, thus indicating the number of files in the directory.alias dir='ls -al'
$alias count='dir | wc -l'
$count
34 $
The shells provide a quoting mechanism that can be used to prevent commands being evaluated under some circumstances.
The shells keep track of alias expansion; an alias which contains its own name will only be expanded once, and aliases are not expanded if they are quoted. This prevents the shell from getting trapped in an infinite loop if it expands an alias that refers to itself.
Note that there are some drawbacks to using aliases. It is easy to accidentally redefine standard commands so that they act in nonstandard or unexpected ways. When you define an alias, it is important to make sure that no program with the same name as the alias already exists; otherwise the alias will be substituted for it. For an example of a dangerous alias (that you should not use) alias -x kill='rm' could have unexpected results if you were to subsequently try to kill a runaway process.
Aliases can include references to variables, but note that you
should enclose the alias definition in single quotes to prevent the
variable from being expanded as the alias is defined. For example:
alias random='echo $RANDOM'
This command evaluates the variable RANDOM.
alias random="echo $RANDOM"
In this example, the alias would have been defined with whatever the literal value of RANDOM was at the time. This is because the shell expands variable references found in double quotes, but not in single quotes. Whenever the variable RANDOM is referenced, it returns a different (random) number. So we would see something like the following:
$You may encounter problems when referring to positional parameters in aliases. Positional parameters are the arguments you specify after a command name; for example, when specifying a filename as a parameter to a command. They are positional in the sense that you identify them as variables by their position along the command line. $0 refers to the name of the program; $1 is the first positional parameter, and refers to the first argument, $2 refers to the second argument, and so on. See ``Creating a shell script'' for more information about positional parameters.alias random='echo $RANDOM'
$random
56302 $random
17094 $alias random="echo $RANDOM"
$random
12916 $random
12916 $
If you use a positional parameter in an alias, the alias will expand the positional parameter of the currently running shell. Aliases are not separate programs and do not have parameters; they are simply replaced with the appropriate string on the command line. For example (in the Korn shell):
$The command set -- sets the positional parameters of the shell. What is happening is that the alias is expanded to the following:set -- bill ted mary
$alias args='echo $3 $2 $1'
$args foo bar quux
mary ted bill foo bar quux $
echo $3 $2 $1 foo bar quuxThe first three names are positional parameters that refer to the first three arguments to the current shell; the additional names are simply tagged on to the end of the echo statement.
If you need to modify the Korn shell positional parameters, use the set -- command. The arguments to set -- are used to replace the shell positional parameters $1, $2 ... $n.
If you want to define a command within the shell that accepts a parameter, you must define a function. A function is a block of commands that are referred to by a name, and that take positional parameters. For example:
del() { rm -i "$@" }When you type del file1 file2 the command line calls the function del(). The shell executes the instructions defined in del(), then resumes execution where it left off: in this case, back at the shell prompt. (You can also use functions in shell scripts.) Parameters to del are passed in the shell positional parameters $1, $2, and so on. $@ is a special parameter consisting of all the positional parameters presented as a single string; so referring to this parameter allows us to interactively remove a variable number of files with one del command. (See ``Passing arguments to a shell script'' for more information on special parameters.)