Using the Korn Shell

This little paper describes how to use the Korn Shell in the UNIX environment (very basic stuff).

Getting Started

At first, you have to create the file .profile in your home directory if it does not yet exist. That file should contain at least the following stuff:

export EDITOR

If you do not have such a file, some things may work but you will not have fun. Make sure it exists. To set (update) the environment variables type the following at the command line:

. ~/.profile

Alternatively, you can start a new session.


Enabling Command-Line Editing

Within ksh there are two editing modes, emacs-mode and vi-mode. Since I like emacs I will explain that mode below.

In order to enable emacs-mode we set the environment variable VISUAL. Put the following line into your .profile:

VISUAL=$(whence emacs)

Update your .profile (see above) and emacs-mode is enabled.


Emacs Editing Mode

Now I will show some important commands within emacs-mode.

Table 1. Emacs-mode commands
Command Description
ESC b Move one word backward
ESC f Move one word forward
ESC h Delete one word backward
ESC d Delete one word forward
CTRL-A Move to beginning of line
CTRL-E Move to end of line
CTRL-K Delete forward to end of line
CTRL-P Move to previous line of history file
CTRL-N Move to next line of history file
ESC < Move to the first line of history file
ESC > Move to last line of history file

There are many more commands but in my opinion they are not so important.


Using Aliases

Aliases are very popular and easy to use. An alias is defined at the command line or within the .profile file. It is simply a synonym for a command. OK, lets start with an example.

alias search=grep

Add this line to your .profile and whenever you type search you will start the program grep. That's it. It is also possible to define more complex aliases:

alias conn='netstat -a --tcp | grep ssh'

Notice that there are no spaces on either side of the equal sign.


I/O Redirectors

Redirection is a very important thing and there is often much confusion about it. Therefore, I will give an overview.

Table 2. I/O redirectors
Redirector Function
> file Direct standard output to file
< file Take standard input from file
cmd1 | cmd2 Pipe
>> file Append to file
n> file Direct output file descriptor n to file
n< file Set file as input file descriptor n
<&n Duplicate standard input from file descriptor n
>&n Duplicate standard output to file descriptor n

The last four entries in the table above require an understanding of file descriptors. If you do not know what a file descriptor is you should read some documentation first.

A very basic fact is that every process has three file descriptors open:

How to save error messages from a program into a single file? OK, let's see an example.

program 2> file

All errors from program go into file.

program > file 2>&1

"2>&1" sends the standard error to the same place as standard output, to file.


Completion and Expansion

I am sure you know that there is a powerful feature, filename completion and expansion. You have the choice between three possibilities within ksh

The first two items are important.


The TAB command is very important and you will often use it. OK, let's have an example.

Assume you want to change the current directory. The new one should be foobar. But there is another directory as well named forms. You type

cd fo TAB

and nothing happens. The reason is that fo matches both directories, foobar and forms. You have to type at least

cd foo TAB

and the directory name will be completed to foobar.


If you want to see some alternatives you use the command ESC *. Related to the example above, to show all directories (or ordinary) files beginning with fo type

cd fo ESC *

Note: ESC * does not execute a command.



Rosenblatt, Bill; Robbins, Arnold: Learning the Korn Shell, Second Edition, Beijing: O'Reilly & Associates, 2002

Back    Top

Created: 2006/08/10
Last modified: $Date: 2006/10/14 17:24:27 $
Copyright © 2006-2008 Lars Knösel