Previous Section  < Day Day Up >  Next Section

11.5. Aliases

An alias is a Korn shell or user-defined abbreviation for a command. The alias name contains alphanumeric characters. Default aliases are provided by the shell and can be redefined or unset. Unlike the C shell aliases, the Korn shell does not support passing arguments. (If you need to use arguments, see "Functions" on page 639.)

Aliases can be exported to subshells by storing them in the ENV file. (The commands in the ENV file are executed every time a new shell is spawned.) Starting with the 1988 version of the Korn shell, the –x option allows aliases to be exported to subshells as long as the new shell is not a separate invocation of ksh. Tracked aliases are provided by the Korn shell to speed up the time it takes the shell to search the path. Aliases can alias themselves; that is, they are recursive.

11.5.1 Listing Aliases

The alias built-in command lists all set aliases.

Example 11.21.

1   $ alias

2   autoload=typeset –fu

3   false=let 0

4   functions=typeset –f

5   hash=alias –t

6   history=fc –l

7   integer=typeset –i

8   r=fc –e –

9   stop=kill –STOP

10  suspend=kill –STOP $$

11  true=:

12  type=whence –v


EXPLANATION

  1. The alias command, without arguments, lists all aliases. This is a list of preset aliases, including those you have set.

  2. The autoload alias is used for invoking functions dynamically.

  3. The false alias is used in expressions testing for a false condition.

  4. The functions alias lists all functions and their definitions.

  5. The hash alias lists all tracked aliases.

  6. The history alias lists and numbers all commands in the history file, .sh_history.

  7. The integer alias allows you to create integer-type variables.

  8. The r alias lets you redo a previous command from the history list.

  9. The stop alias causes a process to be suspended if a job number or PID is provided to the kill command. The job can be resumed in the foreground by typing fg.

  10. The suspend alias suspends the current job by sending the STOP signal and the PID ($$) of this process to the kill command.

  11. The true alias is set to the do-nothing command, often used to start an infinite loop.

  12. The type alias indicates the type of command you are executing: an alias, a binary executable, and so forth.

11.5.2 Creating Aliases

The user can create aliases in the Korn shell. An alias is a nickname for an existing command or commands. The real command(s) is substituted for the alias when the shell evaluates the command line.

Example 11.22.

1   $ alias cl='clear'

2   $ alias l='ls –laF'

3   $ alias ls='ls -aF'

4   $ \ls ..


EXPLANATION

  1. The alias cl is an alias for clear.

  2. The alias is l. The letter l is a nickname for the command ls –laF.

  3. The alias ls is assigned the command ls –aF.

  4. The backslash turns off the meaning of the alias for the execution of this line. The real ls command, not the alias, is executed.

11.5.3 Deleting Aliases

The unalias command deletes an alias.

Example 11.23.

unalias cl


EXPLANATION

The alias cl is removed from the list of set aliases.

11.5.4 Tracked Aliases

To reduce the amount of time needed to do a search of the path, the Korn shell creates an alias when a command is first encountered and sets the alias equal to the full pathname of the command. This is called a tracked alias.[3]

[3] Tracked aliases will be undefined if the PATH variable is reset.

The Korn shell has some preset tracked aliases that are defined when it is installed. To use tracked aliases, the set –o trackall command is issued; it is normally set in the ENV file. To see all tracked aliases, type alias –t.

Example 11.24.

$ alias –t

chmod=/bin/chmod

ls=/bin/ls

vi=/usr/ucb/vi

who=/bin/who


EXPLANATION

The –t option to the built-in alias command displays those commands that have been aliased via the tracking mechanism. When the user types any of these commands, the shell will not search the path, but use the alias definition to invoke the command.

    Previous Section  < Day Day Up >  Next Section