How to Use aliases in ksh shell

Korn Shell Aliases

An alias is a name that you give to a command. You can create an alias in the Korn shell. Aliases are not available in the Bourne shell. Commands, scripts, or programs that require a user to perform a lot of typing to execute them are good alias candidates. Other reasons for using aliases may include:

1. Several versions of a command exist on a system, and you want to use a particular one by default. You create an alias that lists the entire path name to that command:

alias mycommand=/fullpathname/cmd

2. You frequently and incorrectly spell a command, so you make an alias of the incorrect spelling:

alias mroe=more

3. You set up default options for a command:

alias dk=’df -k’

Removing Aliases

If you don’t want to use an alias, delete it using the unalias command. The syntax is:

unalias aliasname

Alias Inheritance

Aliases are not inherited by subshells. Placing the aliases in the $HOME/.kshrc file and having the value of ENV set to $HOME/.kshrc allows new Korn shells to know about the aliases.

$ alias lpgut=’lp -d guttenberg’
$ alias lpgut
lpgut=’lp -d guttenberg’
$ ksh
$ alias lpgut
lpgut alias not found
$ exit
$ alias lpgut
lpgut=’lp -d guttenberg’
$ unalias lpgut
$ alias lpgut
lpgut alias not found

Automatic (Tracked) Aliases

After it is enabled, the Korn shell automatically makes an alias to the full path name of the command whenever you use a UNIX command. If you use the command later, the alias is used instead of searching $PATH. To turn on this Korn shell feature, use the following syntax:

$ set -o trackall
$ date
Thur Mar 14 12:10:00 EST 1991
$ alias
date=/usr/bin/date

The aliases are listed alphabetically, so you might need to search through the list to find the alias you want. If a Bourne shell user types “alias,” the user executes /bin/alias, which lists the Korn shell-predefined aliases. This happens because /bin/alias is a shell script that:

  • Executes in a Korn subshell; therefore, it knows all predefined ksh aliases
  • Sets a variable named cmd to “alias”
  • Executes the value of the variable cmd, passing to it all arguments passed to /bin/alias

Built-in Aliases

You can use the functions and autoload aliases with function names. Use the r alias to re-execute a previously given command. If the r alias is followed by a number, it re-executes the command from the history file that has that number as its command number. If it is followed by a string, it re-executes the most recent command that begins with the string given.

The table below lists some Korn shell predefined aliases.

Command Meaning
functions=‘typeset-f’ Use this alias to list names of functions, with their definitions, that are known in the current shell.
history=‘fc -l’ Displays a list of the 16 most recently given commands. Each command is preceded by its command number.
integer=‘typeset -i’ Use the integer alias to declare a variable as an integer data type.
nohup=‘nohup ' Use to run a command that is immune to the hangup (HUP) and terminate (TERM) signals. If standard output is a terminal, the output is redirected to the nohup.out file. Standard error is redirected to follow standard output.
r=‘fc -e -’ Use this alias to re-execute a previous command.
suspend=‘kill-STOP$$’ Issues the kill command with the STOP(SIGSTOP) signal, Signal 23, on the current shell ($$). The shell process is placed on the jobs list and might be restarted later with the fg command.