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
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:
will re-issue the previous command. To re-execute command 13,
To execute the last command that started with a "c ",
THE fc COMMAND
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:
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:
VI OR EMACS DIRECTIVES
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:
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:
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.
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:
Here are some example aliases:
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:
"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:
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:
Reference: Korn, D.G. and Bolsky, M.I., The KornShell Command and Programming Language, 1989.