Korn Shell (ksh) Programming

This is the top level of my "Intro to Korn shell programming" tree. Korn shell is a 'shell-scripting' language, as well as a user-level login shell. It is also a superset of a POSIX.1 compliant shell, which is great for ensuring portability.


Why scripting?

Scripting, when done right, is a fast, easy way to "get the job done", without the usual "code,compile,test,debug" overhead of writing in C or some other compiled language. It is easier than C for multiple reasons:

  1. Scripting commands tend to be more readable than low-level code. (with the exception of perl)
  2. Scriping languages tend to come with powerful tools attached
  3. There is no "compile" phase, so "tweaking" can be done rapidly.
UNIX tends to take #2 to extremes, since it comes standard with "powerful tools" that can be strung together with pipes or other mechanisms, to get the result you want, with a short development time. It may not be as efficient as a fully compiled solution, but quite often it can "get the job done" in a few seconds of run time, compared to 1 second or less for a compiled program.

A quick scripting solution can be used as a prototype. Then, when you have been using the prototype happily for a while, and you have evolved the behaviour your end users are happiest with, you can go back and code a faster, more robust solution in a lower-level language.

That is not to say that scripts cannot be robust! It is possible to do a great deal of error checking in scripts. Unfortunately, it is not common practice to do so.


What can you do with ksh?

A heck of a lot!

You have access to the full range of UNIX utilities, plus some nifty built-in resources.

Generally speaking, UNIX scripting is a matter of using the various command line utilities as appropriate, with the shell as a means of facilitating communication between each step.

Unfortunately, running all these separate 'external' programs can sometimes result in things working rather slowly. Which is why ksh has a few more things "built in" to it than the older 'sh'.

Why ksh, not XYZsh for programming?

Bourne shell has been THE "standard" UNIX shellscript language for many years. However, it does lack some things that would be very useful, at a basic level. Some of those things were added to C-shell (csh) later on. However, csh is undesirable to use in programming, for various reasons.

Happily, ksh adds most of the things that people say csh has, but sh does not. So much so, that ksh became the basis for the "POSIX shell". Which means that all properly POSIX-compliant systems MUST HAVE something compatible, even though it is now named "sh" like the old Bourne shell. (For example, /usr/xpg4/bin/sh, is a LINK to /bin/ksh, on solaris!) More precisely, the behaviour of a POSIX-compliant shell is specified in "IEEE POSIX 1003.2"

BTW: "Korn shell" was written by "David Korn", around 1982 at AT&T labs. You can now freely download the full source from AT&T if you're not lucky enough to use an OS that comes with ksh already. It's "open source", even!


Chapter headings

Now we get to the good stuff. Please note: you should read these in order. If you do not understand all the stuff from the previous chapters, you may get very confused.

Translations

I am happy to note that my KSH tutorial has been translated into Bulgarian, and Serbo-Croation .
Please let me know of any other translations.
This material Copyright 2000-2011 Philip Brown
Part of bolthole.com... Solaris tips ... AWK tutorial ... OO Programming tutorial