In so many words, these are different dialects of the shell programming language.
Briefly, ksh
should be used for running scripts which were designed to use that dialect, and optionally scripts which were designed to be run with sh
. If you don't know, it should always be a safe option. But normally you use sh
to run scripts which were created for this variant of shell script.
sh
is the Bourne shell (or in your case, the Solaris implementation of the Bourne shell). More generally, on modern systems, sh
would be expected to implement the POSIX shell language specification, though this is hard to guarantee on an unfamiliar platform.
ksh
is the Korn shell, which is backwards-compatible with the Bourne shell. It adds a number of extensions to the base language, many of which are now widely copied by other competing shells. Wikipedia has some additional notes on the history and different versions of ksh
.
Digression: Other Shells
The main competitor is bash
, which has loaned many features from ksh
directly, and implemented many others which are more or less inspired by ksh
(or csh
, or zsh
, or other competitors).
Increasingly, zsh
is now also gaining momentum.
The ugly ducklings are the C shell csh
and its big brother tcsh
, which are not Bourne-compatible, and for that reason increasingly marginal. There was a time when there was less competition, and the features of csh
were compelling enough that it accumulated a large following for a while, but that was 30 years ago.
Historically, there was also the Thompson shell, which was the standard shell on Unix until it was superseded by the Bourne shell in v7 Unix. The Programmer's Workbench had a backwards-compatible derivation called the Massey shell.
Among modern shells, dash
and ash
are relatively faithful reimplementations of the POSIX shell specification. There is also pdksh
which started life as a public-domain reimplementation of ksh
, but now that ksh
itself has a fairly lenient license, it is mainly historical (to my understanding).
Compatibility Notes
In general, given a file script
, running ksh script
should work flawlessly regardless of whether script
was written for Bourne / POSIX shell or specifically for ksh
. Similarly, Bash and zsh (in compatibility mode), or any other shell which claims to be compatible or backwards-compatible with the Bourne shell should be able to run a Bourne shell script just fine.
The opposite does not hold. You will get syntax errors, undefined behavior, or silently wrong results if you attempt to run a (proper) ksh
(or bash
, or zsh
, or etc) script in sh
, or attempt to run a Bash script in Ksh, or vice versa, or etc.
Unfortunately, "Bourne shell" is not entirely well-defined, or rather, a moving target. In modern parlance, usually sh
means the latest POSIX standard, but Solaris in particular was for a long time not compliant with the POSIX specification. And of course, the specification is updated every few years, too. And historically, the original v7 Bourne shell was itself a stable but slowly developing benchmark by itself, especially before POSIX compatibility became the norm.
There are two main variants of ksh
, called ksh88
and ksh93
. Generally speaking, ksh93
can run ksh88
scripts completely transparently, whereas again, the opposite cannot be expected to work reliably.
Important Ksh features
At the time, the Bourne shell had very crude features for interactive use, so the Korn shell's attraction was strong. Its main competitor at the time was the C shell, which had a number of similarities in terms of feature parity, but of course gave up on Bourne-compatible syntax (in favor of what is traditionally called a "C-like" syntax; but I like to say that if that's C-like, then Marilyn Manson is "Marilyn-like"). The Korn shell site itself says ksh
has "the best of both worlds", i.e. Bourne syntax and Csh convenience features, crudely speaking.
The hot features in the late 1980s were interactive command-line editing and history, directory history, job control, and various extensions to the shell language's syntax. Perhaps the most prominent feature loaned into Bash from ksh
is the array variable type. At the time, command aliases, arithmetic support, and built-in echo
and test
commands were also touted as useful features, though the Bourne shell has also been updated to include these features (and aliases are inferior to shell functions anyway).
https://tldp.org/LDP/Bash-Beginners-Guide/html/x7369.html has a brief but rather basic comparison of features between the original Bourne shell, Bash, ksh
, and csh
.
From the #shell
Chat Archives ...
ken> hey here's Unix (-: (1971)
ken> try the shell, it's pretty simple but I think you'll like it
ken> and you know, it's a user-space process, so you can replace it if you like
Doug McIlroy> how about redirection? (1973)
ken> hang on ... we have redirection
bwk> with some pride, we bring you PWB (1977)
pjp> lots of useful utilities in a convenient package
researchers> lol wow
Massey> now with an enhanced shell
Massey> you're welcome
BillJoy> ive been thinking about a new shell for 2BSD (1979)
srb> I have been thinking about a new shell for v7
BillJoy> behold! csh!
srb> I bring you ... sh
freak> I like the features of csh, but ...
n00b> I like the syntax of the Bourne shell, but ...
{?}> srb: the tricks with the C preprocessor freak me out
dgk> look: ksh88 (1988)
AT&T> let's use that
Sun> let's use that
rms> but ... it's not free (1989)
rms> @BrianFox have a look
BrianFox> announcing GNU Bash, the Bourne Again Shell
Linus> here's Linux 0.12, the default shell is Bash (1991)
the world> let's use that
POSIX> new version 1003.2-1992 out now (1992)
POSIX> uses ksh88 as the reference for the shell implementation
dgk> hold my beer ... (1993)
dgk> okay, here's ksh93
the world> but ... it's still not free
AT&T> there is now this super weird license (2000)
the world> mhm ... "decumbered" you say?
AT&T> okay, let's try again: ksh93 is now EPL (2005)