KornShell

Also known as: ksh

On this page 7

KornShell, usually invoked as ksh, is a Unix command interpreter and scripting language created by David Korn at Bell Labs in the early 1980s, combining the scripting syntax of the Bourne shell with interactive features borrowed from the C shell. On Linux, it is an optional shell installed from distribution packages, valued mainly for running legacy enterprise scripts and for its strict, fast, standards-oriented behavior.

KornShell matters beyond its own user base because much of its design became the POSIX shell standard. Features that Bash and Zsh users take for granted, such as $(...) command substitution, arithmetic expansion and command-line editing modes, were popularized or first standardized through ksh.

How KornShell evolved into its current versions

KornShell exists in several distinct implementations, and knowing which one is installed explains most behavioral differences. The original ksh88 became the reference for the POSIX shell specification, while ksh93 added floating-point arithmetic, associative arrays, compound variables and a built-in library model. Because AT&T code was long proprietary, free clones appeared, most notably the Public Domain Korn Shell (pdksh) and its descendant mksh.

The table below summarizes the main variants an administrator may meet on Linux and other Unix systems.

ImplementationOriginTypical use today
ksh88Bell Labs, 1988Basis of POSIX sh; found on older commercial Unix
ksh93AT&T Research, 1993 onwardDefault ksh package on most Linux distributions
ksh93u+mCommunity maintenance forkActively maintained ksh93 with bug fixes
pdkshPublic domain cloneHistorical; replaced by successors
mkshMirBSD Korn ShellAndroid system shell and lightweight systems
OpenBSD kshpdksh derivativeDefault user shell on OpenBSD
Main KornShell implementations

After AT&T released ksh93 under an open source license, development stalled for years, and a short-lived rewrite was abandoned. The ksh93u+m project now carries the original code forward, and current Linux packages generally ship it as /bin/ksh.

Core features of the KornShell language

KornShell provides a full programming language inside the shell, which reduces the need to call external utilities such as expr, awk or sed for routine work. Its built-in arithmetic with `(( ))`, typed variables via `typeset`, and pattern matching with `[[ ]]` let scripts run faster because fewer processes are created.

  • Command-line editing in vi or emacs mode, enabled with set -o vi or set -o emacs.
  • Command history stored in a file named by HISTFILE, with the fc built-in for editing and re-running commands.
  • Indexed and associative arrays in ksh93, declared with typeset -a and typeset -A.
  • Coprocesses started with |&, allowing a script to exchange data with a background process through print -p and read -p.
  • Discipline functions in ksh93 that run code when a variable is read or assigned.
  • Floating-point arithmetic in ksh93, which Bash lacks without external tools.

The print built-in is a KornShell idiom that avoids the portability problems of `echo`, whose handling of backslashes differs between systems. Interactive startup is controlled by the `ENV` variable, which names a file, conventionally ~/.kshrc, read by every interactive shell, while login shells first read /etc/profile and ~/.profile.

KornShell compared with Bash and Zsh

KornShell, Bash and Zsh share a common Bourne-family syntax, so simple scripts often run unchanged in all three, but their priorities differ. Bash dominates Linux as the default login shell, Zsh focuses on interactive comfort, and ksh93 emphasizes execution speed and language features for scripting.

Aspectksh93BashZsh
Default on LinuxRarelyUsuallyRarely; default on macOS
Floating-point mathBuilt inNot availableBuilt in
Associative arraysYesYes, since 4.0Yes
Subshell for last pipe stageNo; runs in current shellYes, unless lastpipe is setNo
Interactive extrasBasic editing and historyModerateExtensive completion and themes
Script startup speedFastModerateModerate
KornShell, Bash and Zsh at a glance

One practical difference surprises many migrating users: in KornShell, the last command of a pipeline runs in the current shell, so command | read var leaves var set afterward. In Bash, the same construct sets the variable inside a subshell and the value is lost unless the lastpipe option is enabled.

Using KornShell on a Linux system

Installing KornShell on Linux is a single package operation, after which scripts select it through a shebang line such as #!/bin/ksh or #!/usr/bin/env ksh. A user can make it the login shell with chsh -s /bin/ksh, provided the path is listed in /etc/shells.

The following decision table helps choose when ksh is the right tool rather than Bash or a plain POSIX shell.

SituationRecommended shellReason
Migrating scripts from AIX, Solaris or HP-UXksh93Those systems used ksh as the default shell
New script for many Linux hostsBash or POSIX shBash is preinstalled almost everywhere
Heavy arithmetic in a shell scriptksh93Native floating-point avoids calls to bc
Embedded or minimal systemmksh or dashSmall binaries with low memory use
Daily interactive workZsh or BashRicher completion and plugin ecosystems
When to choose KornShell

Before relying on ksh-specific features, check the installed version with echo ${.sh.version} in ksh93 or ksh --version. Scripts meant for broad portability should avoid ksh93-only syntax such as compound variables and stick to the POSIX subset that every Bourne-family shell supports.

Limitations and common pitfalls of KornShell

The biggest operational risk with KornShell is assuming that every `ksh` is the same program. A script written for ksh93 may fail under mksh or OpenBSD ksh, which do not implement floating-point arithmetic, compound variables or several ksh93 built-ins.

SymptomLikely causeFix
Script works on AIX but fails on Linuxksh88 versus ksh93 differencesTest under ksh93 and adjust deprecated constructs
Arithmetic error on decimal valuesRunning under mksh or pdkshInstall ksh93 or use awk for decimals
~/.kshrc settings ignored`ENV` variable not setExport ENV=$HOME/.kshrc in ~/.profile
Bash script misbehaves under kshBash-only syntax such as {1..5} assumptionsRewrite with portable loops or keep Bash
Function variables leak to callerPOSIX-style function definitionUse function name { } syntax for local scope
Typical KornShell problems and fixes

Another subtle point concerns function scoping. In ksh93, only functions declared with the function keyword get local variables through typeset; functions written as name() { } follow POSIX rules and share variables with the caller. Finally, ksh offers a smaller interactive ecosystem than Zsh or Bash, with fewer completion scripts and community frameworks.

Explained without jargon

A shell is the program that reads the commands you type and tells the computer what to do. KornShell is one of these programs, created at Bell Labs decades ago. It sits in the same family as Bash, the shell most Linux users meet first, and it understands mostly the same commands.

Its special strength is writing automated scripts that run quickly and reliably. Many large companies built years of automation on it, so it is still installed today to keep those scripts working. Think of it as an experienced older sibling of Bash: less fashionable, but dependable and fast.

A typical use case

A bank is moving its nightly batch jobs from an aging AIX server to Red Hat Enterprise Linux. The jobs consist of about 300 KornShell scripts that move files, check totals and load data into a database. Rewriting them in Bash would take months and introduce risk, so the team installs the ksh package, which provides ksh93u+m, and keeps the #!/bin/ksh shebang lines unchanged.

Testing reveals that around 15 scripts rely on ksh88 behavior, such as old-style arithmetic and obsolete options, and these are corrected in a week. The rest run unmodified, and several jobs finish faster because ksh93 handles decimal calculations internally instead of calling bc. The migration completes on schedule with the existing automation intact.