Korn Shell Alias Utility

An alias is a shorthand notation in the Korn shell to enable you to customize and abbreviate UNIX commands. An alias is defined by using the alias command.

3>Command Format

The alias command syntax is as follows:

alias name=command_string

For example:

$ alias dir='ls -lF'

The shell maintains a list of aliases that it searches when a command is entered. If the first word on the command line is an alias, the shell replaces that word with the text of the alias. When an alias is created, the following rules apply:

  • There can be no space on either side of the equal sign.
  • The command string must be quoted if it includes any options, metacharacters, or spaces.
  • Each command in a single alias must be separated with a semicolon.

Predefined Korn Shell Aliases

The Korn shell contains several predefined aliases, which you can display by using the alias command. User-defined aliases are also displayed.

$ alias
autoload='typeset -fu'
command='command '
functions='typeset -f'
history='fc -l'
integer='typeset -i'
local=typeset
nohup='nohup '
r='fc -e -'
stop='kill -STOP'
suspend='kill -STOP $$'

User-Defined Aliases

Aliases are commonly used to abbreviate or customize frequently used commands. For example:

$ alias h=history $ $ h 278 cat /etc/passwd 279 pwd 280 cp /etc/passwd /tmp 281 ls ~ 282 alias h=history 283 h

Using the rm, cp, and mv commands can inadvertently result in loss of data. As a precaution, you can alias these commands with the interactive option.

For example:
$ alias rm='rm -i'
$ rm dat1
rm: remove  dat1: (yes/no)? no
$

By creating a cp -i and mv -i alias, the shell prompts you before overwriting existing files.

You can deactivate an alias temporarily by placing a backslash () in front of the alias on the command line. The backslash prevents the shell from looking in the alias list, thereby causing it to perform the original rm command.

For example:

$ rm file1 rm: remove file1 (yes/no)? no $ $ rm file1 $ ls file1 file1: No such file or directory

Command Sequences

You can group several commands together under a single alias name. Individual commands are separated by semicolons. For example:

$ alias info='uname -a; id; date' $ info SunOS host1 5.8 Generic sun4u sparc SUNW,Ultra-5_10 uid=102(user2) gid=10(staff) Fri Jun 30 15:22:47 MST 2000 $
In the next example, an alias is created using a pipe (|) to direct the output of the ls -l command to the more command. When the new alias is invoked, a directory listing appears. If the listing is longer than the available space on the screen, the --More-- line item appears at the end of the list, indicating that additional directory contents are displayed on the subsequent screen.

For example:

$ alias ll='ls -l | more' $ cd /usr $ ll total 136 drwxrwxr-x 2 root bin 1024 May 13 18:33 4lib drwx------ 8 root bin 512 May 13 18:14 aset drwxrwxr-x 2 root bin 7168 May 13 18:23 bin drwxr-xr-x 4 bin bin 512 May 13 18:13 ccs drwxrwxr-x 5 root bin 512 May 13 18:28 demo

Removing Aliases

Use the unalias command to remove aliases from the alias list. The unalias command syntax is as follows:
$ unalias alias_name

For example:

$ unalias h
$ h
ksh: h:  not found

Note: To pass the new aliases to every shell invoked, place it in your Korn shell initialization file (usually called .kshrc).

Other User Korn Shell Variables

Other Korn shell variables are described in the table.

Other Korn Shell Variables
Variable Use
LPDEST
Set your default printer. Use this value if using the /usr/bin/lp command.
PRINTER
Set your default printer. Use this variable if using the lpd-based compatibility commands (lpr, lpq, and lprm)
TERM,
Define the terminal. This variable should be reset in the /etc/profile file. When the user invokes an editor, the system looks for a file with the same name that is defined in this environment variable. The system searches the directory referenced by TERMINFO to determine the terminal characteristics.
MANPATD
Set the hierarchies of man pages that are available.

Leave a Comment

Your email address will not be published. Required fields are marked *

CAPTCHA * Time limit is exhausted. Please reload the CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top