Previous Table of Contents Next


Module 4
alias/unalias (csh, ksh)

DESCRIPTION

The internal alias command provides a shorthand for one or more commands. The alias command is supported by the csh and the ksh. There are three types of aliases in the Korn shell: exported, preset, and tracked. An exported alias is passed to subshells when called by name. The preset aliases are set by the Korn shell. The tracked aliases are full pathnames for commands.

Aliases can be used to redefine the shell's built-in commands but not the keywords.

Aliases can be created, exported, and listed using the alias command. Aliases can be removed using the unalias command. Exported aliases are passed to subshells. Therefore, if you have an alias defined in your login shell and you execute a shell script, the shell script is able to access the aliases.

Aliases are established as they are read, not during execution. For an alias to work, it must be created before the command that uses the alias is executed. Basically, you have to define your aliases before you can reference them.

The internal unalias command is used to remove alias definitions. For example, if you want to do away with your l alias, you enter unalias l and press Return.

Aliases can be created, exported, and listed using the alias command. Aliases can be removed using the unalias command.

COMMAND FORMAT

Following is the general format of the alias command.

   alias [ -tx ] [ name=string ... ]
   alias name
   unalias name

If no options or arguments are given, then all existing aliases are listed.


C Shell (csh)
alias name string
unalias name

Options

The following options may be used to control how alias functions.

-t Allows you to set and list tracked aliases. A tracked alias has a full pathname for the command used in the string. This provides less overhead in search and execute time since the location of the command is already known. If your PATH variable is reset, the string of a tracked alias is reset to NULL. If no arguments are provided on the command line, all tracked commands are listed - not really an alias, but it is how the Korn shell handles tracking commands.
-x Allows you to set or list exported aliases. An exported alias functions like an exported variable; it may be passed to subprocesses. If no arguments are provided on the command line, all exported aliases are listed.

C Shell
The csh alias does not support any options.

Arguments

The following list describes the arguments that may be passed to the alias command.

name=string Defines an alias name that runs the command string.
name The name of the alias. The name becomes a new command that is an alias for the command specified in the string part of the alias. For example,
      alias lx="ls -x"
sets the name lx to the command ls -x specified in the string. Now you can use lx as a command.
If only a name is provided, then the alias string for that alias is displayed if it exists.
= The delimiter between the name and command string.
string The command you wish to have executed when you use name as a command. If any spaces or tabs are contained in the string, then you must quote the entire string. For example,
      alias dir="ls -la | pg"
sets dir to be an alias for the command ls -la | pg. So when you type dir and press Return, you actually execute the ls -la | pg command, the string part of the alias.
If a space follows the string of an alias command, then the following commands are checked for being an alias.
If no names or strings are given, then the appropriate aliases are listed with their corresponding string.
If there are spaces in the string side of the alias, you must enclose it in quotes.

C Shell (csh)
The C shell uses a slightly different syntax to set up an alias. Instead of using an equal sign (=), you simply put a space between the name and the command. The string does not have to be enclosed in quotes if it contains spaces.
The C shell alias also allows you to use history substitution which is replaced by command line arguments. For example,
alias la 'echo \!*'
would expand la * into echo file1 . . ., which would be all the files in the current directory.

FURTHER DISCUSSION

If you alias a command that already exists as a true command, you can still refer to it by enclosing it in single quotes. For example, if you set the following alias

   alias ls='ls -x'

you can still run the normal ls by typing

   'ls'

or

   /bin/ls

It is considered best not to rename an existing command using alias unless you always use the command the way you are setting up the alias. For example, if you always use ls -x, you may want to create an alias, ls="ls -x". To keep from interfering with the ls command, you might consider using l or lx for the alias name.

If you wish to have a command aliased each time you log in to the system, you must place the alias command line in your .profile or in your ENV (.kshrc) file. The ENV file is executed each time a ksh is started.

You might find the following list of aliases useful.

   alias -x lf="ls -Fx"
   alias -x l="ls -l"
   alias -x ud="cd .."
   alias -x bd="cd -"
   alias -x dir="ls -l"
   alias -x dir/w="ls -x"
   alias -x copy="cp "
   alias -x more="pg -n "
   alias -x type="cat "   # although type is an internal command
   alias -x prt="lp -dlaser"   # alias to whence using print is not advisable

Exported aliases

Exported aliases are like exported variables. They are passed to subshells. Aliases that have not been exported are not passed to subshells and, therefore, are not known by such subshells. The following aliases are automatically exported by the shell.

   echo='print -'
   false='let 0'
   functions='typeset -f'
   hash='alias -t'
   history='fc -l'
   integer='typeset -i'
   nohup='nohup '
   pwd='print - $PWD'
   r='fc -e -'
   true=':'
   type='whence -v'

Tracked aliases

Tracked aliases are not true aliases. A tracked alias is the actual command name and the full pathname used to access an existing command. It is called tracked alias because the path is aliased for the command name and the new alias tracks directly to the command. This keeps the shell from having to search the entire PATH for a command, thus providing faster command execution.

You can turn command tracking on by using the set command. Type

   cj> set -o trackall

to inform the shell to track all commands issued to it. For example, the following sequence of commands illustrates the automatic tracking of commands.

   cj> set -o trackall
   cj> alias ls             # list alias of ls command
   ls alias not found       # no alias exists for ls
   cj> ls                   # execute the ls command
   ...
   cj> alias ls             # list alias of ls command
   ls=/bin/ls               # tracked alias has been set to full path

You can track individual commands by using the -t option. For example,

   cj> alias -t ls

would cause the same results as the above steps. But the shell would not automatically set every command you issue to a tracked alias.

RETURN CODES

Alias returns a zero (true) unless a name is given that has not been set to a string.

APPLICATIONS

The alias command is used to create shorthand versions of longer commands. It is very useful for hard-to-remember commands and for those commands you use repeatedly throughout the day. Remember to place your aliases in your .profile or your ENV file (.kshrc) so they are set each time you log in.

The unalias command is used to remove previously set aliases.

TYPICAL OPERATION

In this activity you use the alias command to create various aliases. Begin at the shell prompt.

1.  Type alias "I= -F" and press Return.

C Shell
Type alias I "ls -F" and press Return.

2.  Now try your new command by typing I and pressing Return. Notice the output of your l command is a multicolumn list with the executable (*) and directory (/) files being flagged.
3.  List all aliases you have set by typing alias and pressing Return.

In this activity you use the unalias command to remove the previously defined l alias command. Begin at the shell prompt.

1.  Remove the l alias by typing unalias I and pressing Return.
2.  Type alias I and press Return to list out your l alias. Notice the l alias is no longer defined.
3.  Turn to Module 64 to continue the learning sequence.


Previous Table of Contents Next