Introduction to the Korn Shell


The Korn Shell (ksh) is an interactive command interpreter and programming language that has the best features of both the C Shell and the Bourne Shell. It has many of its own features as well, like built-in command line editors and unique shell functions. ksh initializes a file called .profile upon login to set up your environment.

Below is a brief overview on using the Korn Shell. For more complete information on the Korn Shell, type "man ksh".

A "$" represents a command, or shell, prompt; press the Return key after each command. "ESC" is the Escape key. Text in italics is what you actually type in on the system; text in Courier are items displayed on the screen or sample code.


The Korn Shell has new and improved features that make it desirable to use over most shells:

HISTORY MECHANISM:The history command provides a shortcut for users in that previous commands can be reintroduced and possibly modified. You can access these previous commands using the Vi or Emacs editor directives, or by using the command fc. The history file is maintained across login sessions.

COMMAND-LINE EDITING:The Korn Shell has built-in Vi-like and Emacs-like editors so that you may edit the current command line and previous commands.

COMMAND NAME ALIASING:Aliases provide a means of shortening the name of a command or sequence of commands. When you type an alias on the command line, the shell replaces it with the command sequence to which it refers. Aliases effectively allow you to abbreviate commands and to alter the meaning of existing commands from your point of view.

JOB CONTROL:On most systems, jobs run in the Korn Shell can be stopped and moved to and from the background using the bg and fg commands.

NEW CD CAPABILITIES: Users can return to the previous directory without typing its whole pathname. ksh allows you to extend the capabilities of the cd command by replacing it with a user definition.

TILDE EXPANSION: The home directory of any user can be referred to using the tilde (~) character.


A file called .sh_history is kept by ksh that stores each command you enter so that you can re-enter previous commands with ease. To display the most recent commands, type

$ history
22 cd usr/lib
23 mail
24 w
25 vi rick
26 cd
27 trn tamu.general
28 ls -al
29 history |mail

Note that each command has a number associated with it. You can limit the number of commands you want displayed on your terminal if desired:

$ history -10 # displays last 10 commands
$ history 23 26 # displays commands 23 through 26
$ history 24 # displays commands starting at 24


The command:

$ r # like "!!" in the C shell

will re-issue the previous command. To re-execute command 13,

$ r 13 # like "!13" in the C shell

To execute the last command that started with a "c ",

$ r c # like "!c" in the C shell



The fixed command (fc) will give you access to your history file and your favorite editor will bring up the most recent command for editing:

$ fc

The environment variable FCEDIT should be set to your favorite editor in your .profile or on the command line. Here is an example for setting FCEDIT to Emacs:

FCEDIT=/usr/local/bin/emacs; export FCEDIT


You can use emacs or vi directives to edit current and previous commands before re-issuing them.


Usually by default, vi is set as your editor. To use Emacs as your built-in editor, put the following lines in your .profile:

set -o emacs
EDITOR=/usr/local/bin/emacs; export EDITOR
VISUAL=/usr/local/bin/emacs; export VISUAL

Then you can use the same cursor movements you normally use in Emacs, such as Control-n, Control-p, and the arrow keys. Also, try these:

ESC = displays list of pathnames resulting from expansion of word under cursor
ESC ESC appends characters to word under cursor to complete pathname of file
ESC - inserts argument of last command
Control-v gets Emacs version number


You can use the same cursor movements you normally use in vi after you type an ESC to get into the control mode, where you can also edit using vi directives. If you are not sure which mode you are in, go ahead and press ESC.

ESC = displays list of pathnames resulting from expansion of word under the cursor
ESC \ appends characters to the word under the cursor to complete pathname of the file
ESC * replaces word under the cursor with the list of possible pathnames
v invokes vi so you can edit a file containing the current command


Aliasing allows you to redefine the commands used by UNIX. Aliases are usually performed only on the first word of a command. If the command name matches an alias, the command is replaced by the text of the alias. If the last character of the replacing text is a blank, the word following it is also checked for an alias.


Aliases can be set on the command line or in your .profile using this syntax:

$ alias command='text'

Here are some example aliases:

alias h='history'
alias nohup='nohup '
alias a='alias'
a dir='ls -al'
a cls='clear'
a mail='elm'
a rn='trn -XX -x61s'


One interesting feature of the cd command in ksh, "cd -," allows you to return to the previous directory without much typing. Here's an example:

$ pwd
$ cd /tmp1/model; pwd
$ cd; pwd
$ cd -; pwd

"cd -" simply changes the current working directory to the previous working directory. "cd oldstring newstring" substitutes "oldstring" with "newstring" in the working directory name. To print the current directory in your prompt, insert the following in your .profile:

PS1='${PWD}$ '; export PS1


A tilde (~) refers to your home directory when not attached to a userid, and "~abc1234" is a quick way to reference abc1234's home directory. Examples:

$ cd ~/mydir # change to your "mydir" directory
$ cd ~abc1234/herdir # change to abc1234's "herdir"

Reference: Korn, D.G. and Bolsky, M.I., The KornShell Command and Programming Language, 1989.

Last Modified: 05:39pm CST, December 12, 1995