keepcalculator

06 Numbers and Powers

Korn Shell: What It Is & How to Use It

Card No. 06·62910 · Updated · Standard formula, shown below

The Korn shell (ksh) is a Unix command-line interpreter and scripting language that combines the interactive features of the C shell (csh) with the powerful scripting syntax of the Bourne shell (sh). You enter commands or run scripts, and the shell returns output or executes programs; it is known for its speed, job control, and advanced scripting capabilities.

Background: Where Did Korn Shell Come From?

The Korn shell was developed by David Korn at AT&T Bell Laboratories in the early 1980s. It was designed as an improved successor to the Bourne shell (sh), which was the default Unix shell at the time. Korn wanted to keep the Bourne shell’s scripting compatibility but add interactive features like command-line editing, job control, and history, which were popularized by the C shell.

The first public release was ksh88 (1988), followed by ksh93 in 1993. ksh93 added many modern features: associative arrays, floating-point arithmetic, and better string manipulation. Today, the most common open-source implementation is ksh93 (often called ksh on Linux), and there is also a newer ksh2020 that is still in development. The shell is POSIX-compliant, meaning it follows the standard for Unix shells.

How the Korn Shell Works

Like any Unix shell, ksh reads commands from a terminal or a script file and executes them. It has three main modes:

  • Interactive mode: you type commands and see output immediately. ksh provides command-line editing (with vi or emacs key bindings), history recall, and tab completion.
  • Script mode: you run a file containing a sequence of commands. The first line of a script is usually #!/bin/ksh to tell the system which interpreter to use.
  • Login shell: the shell that starts when you log in, reading .profile for setup.

ksh also supports variables, control structures (if, for, while), functions, and arithmetic. For example, a simple script to print numbers 1 to 5:

#!/bin/ksh
for i in 1 2 3 4 5; do
  print $i
done

The print command is ksh’s built-in output command (similar to echo).

Why Choose Korn Shell Over Others?

People choose ksh for several reasons:

  • Compatibility: ksh is backward-compatible with the Bourne shell, so most sh scripts run without changes.
  • Speed: ksh is often faster than bash for loops and arithmetic because it is optimized for that.
  • Advanced scripting: ksh93 supports associative arrays, floating-point math, and built-in regular expressions, which make complex scripts shorter.
  • Interactive features: command-line editing and history are built-in, similar to bash.

However, bash is more widely installed by default on Linux, and many users find bash’s syntax more familiar. ksh is more common on older Unix systems (like AIX) and in environments where POSIX compliance is critical.

Common Problems and Misconceptions

One common issue is that ksh scripts may fail when run with bash because of syntax differences. For example, ksh supports [[ ... ]] for tests, but bash also does; however, some ksh-specific features like typeset or print may not work in bash. Another misconception is that ksh is obsolete—it’s still actively maintained and used in many enterprises.

A frequent problem is that the default shell on Linux is bash, so users may not have ksh installed. You can check with which ksh or echo $SHELL. If ksh is not installed, you can install it via your package manager (e.g., apt install ksh on Debian/Ubuntu).

Another gotcha: ksh’s print command behaves differently from echo in some cases, especially with escape sequences. Use print for reliable output in ksh scripts.

Comparing Korn Shell to Bash and Zsh

Here’s a quick comparison:

  • Bash: most common on Linux, great for interactive use, but slower in loops and lacks associative arrays (until bash 4).
  • Zsh: highly customizable, with advanced completion and theming, but not POSIX-compliant by default.
  • Ksh: POSIX-compliant, fast, and powerful for scripting, but less popular in the Linux desktop world.

If you need to write portable scripts that run on many Unix systems, ksh is a solid choice. If you’re a Linux user who wants a modern interactive shell, bash or zsh may be more convenient.

Safety and Accuracy Tips

When writing ksh scripts, always quote variables to avoid word splitting: print "$var". Use set -u to treat unset variables as errors, and set -e to exit on any command failure. These practices prevent silent bugs.

Also, be careful with arithmetic: ksh uses $((...)) for integer math. For floating-point, you need typeset -f or use bc.

Frequently Asked Questions

Is the Korn shell the same as Bash?

No, but they are similar. Both are POSIX-compliant shells with command-line editing and scripting. Bash has some extra interactive features, while ksh is often faster for scripting and has some different syntax (e.g., print vs echo).

How do I change my default shell to ksh?

You can use the chsh command (e.g., chsh -s /bin/ksh) on Linux, or edit /etc/passwd on some systems. Log out and back in for the change to take effect.

Can I run Bash scripts with ksh?

Sometimes, but not always. Bash has features that ksh lacks (like [[ -v ]] for variable existence), and ksh has features bash lacks. It’s best to write scripts for a specific shell and test them.

What does the #!/bin/ksh line do?

It’s called a shebang. It tells the operating system to use the Korn shell interpreter to execute the script. Make sure the path is correct (use which ksh to find it).

Is ksh still maintained?

Yes. The ksh93 version is maintained by AT&T, and there is an open-source project called ksh2020. It’s still used in many enterprise Unix environments.

How do I install ksh on my system?

On Debian/Ubuntu, use sudo apt install ksh. On Red Hat/CentOS, use sudo yum install ksh. On macOS, you can use Homebrew: brew install ksh.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →