[ Team LiB ] Previous Section Next Section

The Korn Shell

The Korn shell, developed by David Korn of AT&T Bell Laboratories, is a superset of the Bourne shell. That is, the Korn shell uses the same syntax as the Bourne shell, but the Korn shell has more built-in functions that can be defined directly from the shell. The Korn shell provides a more sophisticated form of command editing than does the C shell. The Korn shell also provides a command history and aliases.

The Korn shell provides a complete command and scripting language. The following sections introduce some of the most basic features of the Korn shell.

Reviewing Korn Shell Initialization Files


The Korn shell uses two initialization files in the user's home directory to set the user's environment: .profile and .ksh-env, a file denoted by the ENV environment variable. You might want to name the file .kshrc because its function is similar to that of the C shell .cshrc file.


When the user logs in, the .profile file is read and then the .ksh-env file. The .ksh-env file lets you configure the Korn shell session to your needs. Put environment variable settings into the $HOME/.profile file, and put all aliases, functions, and set -o commands in the .ksh-env file.


You must set the ENV environment variable to point to the .ksh-env file. The syntax for setting environment variables in the Korn shell is the same as for the Bourne shell: VARIABLE=value; export VARIABLE. As in the Bourne shell, you must export the variable to make it inheritable by subsequent shells and programs invoked by the exporting shell. The following example sets the environment variable for a .kshrc file.

$ ENV=$HOME/.kshrc;export ENV

You must set this environment variable in the .profile file; otherwise, the .kshrc file is not found when you log in. The ENV variable has no default setting. Unless you set it, the feature is not used. The . ksh-env file is read each time you start the Korn shell from a command line.

Using Korn Shell Options

The Korn shell has a number of options that specify the user's environment and control execution of commands. To display the current option settings, type set -o and press Return. In the following example, the default options for the Korn shell for the Solaris Operating Environment are displayed.

$ set -o
Current option settings
allexport        off
bgnice           on
emacs            off
errexit          off
gmacs            off
ignoreeof        off
interactive      on
keyword          off
markdirs         off
monitor          on
noexec           off
noclobber        off
noglob           off
nolog            off
nounset          off
privileged       off
restricted       off
trackall         off
verbose          off
vi               off
viraw            off
xtrace           off

The default options are described in Table 29. Customarily, you set these options in the .ksh-env file.

Table 29. Korn Shell Options






Automatically export variables when defined.



Execute all background jobs at a lower priority.



Set emacs/gmacs as the in-line editor.



If a command returns the value False, the shell executes the ERR trap (if set) and immediately exits.



Set gmacs as the in-line editor.



When the interactive option is also set, the shell does not exit at end-of-file. Type exit to quit the shell.



The shell automatically turns the interactive option on so that shell prompts are displayed.



The shell puts each word with the syntax of a variable assignment in the variable assignment list.



Display a / following the names of all directories resulting from path-name expansion.



Enable job control.



Do not overwrite an existing file when the redirect operator (>) is used.



Read commands but do not execute them. You can use this option to debug shell script syntax errors.



Disable file-name expansion.



Do not store function definitions in the history file.



Display an error message when the shell tries to expand a variable that is not set.

graphics/new.gif privileged


When this option is off, the effective UID and GID of the user are ignored and the real UID and GID are used. When this option is on, the shell uses the effective UID and GID of the user.



Set a restricted shell.



Make command-tracked aliases when they are first encountered.



Display the input as it is read.



Set vi as the in-line editor.



Specify character-at-a-time input from vi.



Display commands and arguments as they are executed.

To enable an option, type set -o option-name and press Return. To disable an option, type set +o option-name and press Return.

For example, entering this line in the user's .ksh-env file sets the in-line editor to vi.

set -o vi

The following example turns off vi as the in-line editor.

set +o vi

You can also set these options from a command line, using the same syntax.

Creating Korn Shell Aliases

The syntax for creating aliases for the Korn shell is alias name=value. The following example creates an alias for the alias command.

$ alias a=alias

The following example uses the a alias created in the last example to alias the history command to the letter h.

$ a h=history

The Korn shell comes with a default set of predefined aliases. To display the list, type alias and press Return.

$ alias
autoload=typeset -fu
false=let 0
functions=typeset -f
hash=alias -t -
history=fc -l
integer=typeset -i
r=fc -e -
stop=kill -STOP
suspend=kill -STOP $$
type=whence -v

The default aliases are described in Table 30.

Table 30. Korn Shell Preset Aliases





typeset -fu

Allow function definitions to be deferred until the first time they are invoked. At that time, they are autoloaded on-the-fly.


graphics/new.gif For example, autoload sum; other shell statements; sum 3 7 autoloads the sum function, then does other work. Finally, the command runs the sum function with the arguments 3 and 7. At this point, the shell searches all directories specified in the FPATH environment variable for a file named sum. If found, this file—which should contain the definition for the sum function—is sourced. Only then is the sum function called.

graphics/new.gif command


Used within a function to invoke a built-in shell command or an external program that happens to have the same name as the function so that you don't call the function recursively.


typeset -f

List all currently defined functions.


fc -l

List the last 16 commands that were run in this shell.


typeset -i

Declare integer variable. For example, integer i=7.

graphics/new.gif local


Allow the definition of a shell variable in a function that is local to that function. Without this alias, any shell you define in a function is global to the entire script.


Consider the following example.

function printdate {
  local x=$(date)
  echo ${x}

Inside the function printdate, the local variable x is defined and is assigned the output of the date command. The contents of x are then printed. Note that x is not defined once the printdate function returns.



When used on a program invoked from the shell, this alias prevents the program from receiving the hang-up (HUP) signal. This action prevents the program from being terminated if you log out, as it normally would be.


'fc -e -'

Reexecute the previous command.


kill -STOP

Send the STOP signal to the process with the PID you give as an argument. This alias stops the process until you use the bg or fg commands to allow the process to continue.


kill -STOP $$

Suspend the current shell by sending it the STOP signal. You do this only if you were running a shell, then invoked another shell from within it, and want to temporarily return to the original shell by suspending the second one.

Editing Commands with the Korn Shell In-line Editor

You can use the Korn shell in-line editor to edit the current command before you execute it. You can choose one of three in-line editors: emacs, gmacs, or vi. You specify the in-line editor by using the set -o editor option or by setting either the EDITOR or VISUAL environment variable. This section describes how to use the vi in-line editor to edit commands.

The vi in-line editor is a modified subset of the vi program; it lacks some of the features of vi. The vi in-line editor is automatically in insert mode. You can type commands and execute them by pressing Return without using the vi in-line editor. If you want to edit a command, press Escape to enter command mode. You can move along the command line with the standard cursor movement commands and use standard vi editing commands to edit the contents of the line. When the command is edited, press Return to execute it or press Escape to return to input mode.

If you want to edit the command line in a vi file, type v to open a vi file containing the contents of the command line. When you leave vi, the command is executed. Refer to Table 24 on page 93 for a quick-reference to common vi commands.

Setting the Size of the Korn Shell's History

The Korn shell stores history commands in a file specified by the HISTFILE variable. If the variable is not set, the files are stored in $HOME/.sh_history. You can specify the number of commands stored by using the HISTSIZE variable. If the variable is not set, the most recent 128 commands are saved. When the history list contains the maximum number of commands, then as new commands are entered, the oldest commands become unavailable.

To set a different history size, type HISTSIZE= n;export HISTSIZE and press Return. History is set to the number of lines you specify.

The following example sets the history size to 200.


You can set the history temporarily for a shell window or set it "permanently" by entering the command as a line in the .profile file.

Displaying Korn Shell History Commands

You can use two commands to show the commands from the history list: fc and history. Because history is aliased to fc -l as one of the default aliases, you can use the commands interchangeably. If you do not specify a range with either the history or fc -l command, the last 16 commands are displayed.

To display the last 16 commands in the history list, type history and press Return. The last 16 commands in the history list are displayed.

$ history
    16  pwd
    17  ps -el
    18  ps -el | grep openwin
    19  cd
    20  more questionnaire
    21  su
    22  lp /etc/passwd
    23  lpstat -t
    24  man ksh
    25  du
    26  maker &
    27  tip -2400 5551212
    28  alias h=history
    29  find / -name ksh -print
    30  df -k
    31  history

An alternative way to display the same information is to type fc -l and press Return.

The history and fc commands take additional arguments that let you specify a range, display the last n number of commands, and display the commands in reverse order. See the ksh(1) manual page for more information.

Using Korn Shell History Commands

To use a command from the history list, type r n to reuse a command by number. The following example reuses command 27.

$ r 27
tip -2400 5551212
(Connection messages are displayed)

To repeat the last command in the history list, type r and press Return.

Editing Korn Shell History Commands

You can display individual history commands and edit them by using the fc command with the following syntax.

fc [-e editor] [-r] [range]

The following syntax also works.

fc -e - [old=new] [command]

You use the -e option to specify an editor. If no editor is specified, the FCEDIT environment variable value is used. If no value is set, the default editor is /bin/ed. The -r option reverses the order of the commands, displaying the most recent commands at the top of the list. If you specify no range, the last command is edited.

For example, to use vi to edit the last command in a history list, type fc -e vi and press Return. A vi file is created containing the last entry from the history list. When you edit the command and save the changes, the command is executed.


An excellent Korn shell programming reference is The New Korn Shell Command and Programming Language, Second Edition, by Morris I. Bolsky and David G. Korn, Prentice Hall, 1995.

    [ Team LiB ] Previous Section Next Section