What is Korn Shell (KSH) in UNIX/Linux System

Linux TLDR
Last Updated:
Reading time: 4 minutes

You might have heard of the Bash shell (or even ZSH or Fish), whose popularity has overshadowed other shells to the point that most Linux users think they are the only shell interpreters available for Linux.

But before they came into existence, there was another shell that developed a strong foundation for modern shells.

What is the Korn Shell (KSH) in Linux?

Korn shell (or KSH) is a UNIX shell (that complies with POSIX 2) developed in the late 1980s by David Korn at Bell Labs, way earlier than Bash shell.

Korn Shell (KSH)

Don’t confuse the fact that KSH is an earlier version of Bash (yet it is backward compatible with Bash); they both are different programs, and Bash is based on shell source code.

However, its development goal was to inherit all the features of C shell (csh) and Tab C-shell (tcsh), including scripting and loop handling, which are identical to Bourne shell.

Unlike the echo command in Bash used to print messages in the terminal, it uses the print command (considered better than echo) to do the same job.

Difference Between Korn Shell (KSH) and Bash?

The following is the difference between both of them:

Korn Shell (KSH)Bourne-Again SHell (Bash)
The path for the Korn shell is β€œ/bin/kshβ€œ.The path for the Bash shell is β€œ/bin/bashβ€œ.
The script extension for KSH shell is β€œ.kshβ€œ.The script extension for Bash shell is β€œ.shβ€œ.
It uses the print command to print the message (better than echo) on the terminal screen.It uses the echo command to print the message on the terminal screen.
It supports associative arrays (rarely used).It does not support associative arrays.
It handles the loop syntax way better.Exit codes are handled better.
It does not support tab completion.It supports tab completion.
Considered better for script writing.Best for interactive use cases.

As you can see, with such features, KSH would surely be a good choice back then, but due to Bash being the default shell in most Linux systems, the popularity of KSH just faded over time.

Yet, some systems like Solaris, AIX, and HPUX have KSH as the default, but you will hardly find any articles related to KSH, and even getting started might be difficult for untrained Linux users.

I would highly recommend going with the Bash shell (due to its popularity) if you are a beginner; you can easily find many learning resources for it.

Korn Shell (KSH) Variants

There are several forks and clones of the original Korn shell, followed by:

  • dtksh: Included as part of the common desktop environment (CDE).
  • tksh: Provides access to the Tk widget toolkit.
  • oksh:Β Intended to provide portability across operating systems (default for DeLi Linux 7.2).
  • loksh: Ported by OpenBSD’s variant of Korn shell with minor changes.
  • mksh: Intended for both interactive and shell script use.
  • SKsh: AmigaOS variant.

With that, let’s see the installation of KSH in Linux.

How to Install Korn Shell (KSH) in Linux

By default, Korn shell is available in most Linux repositories, and you can execute one of the following commands to install it, depending on your Linux system.

$ sudo apt install ksh                                                                              #On Debian or Ubuntu
$ sudo dnf install ksh                                                                              #On Fedora or RHEL
$ sudo pacman -S ksh                                                                            #On Arch or Manjaro

Execute the following command to verify the success of the installation:

$ which ksh

#OR

$ ksh --version

Output:

Verifying the Korn Shell (KSH) installation

Now you can directly execute the ksh command to enter in interpreter mode, but if you want to have KSH as your default shell, then execute the following chsh command:

πŸ“
Don’t forget to replace β€œlinuxtldr” with your actual username.
$ sudo chsh -s $(which ksh) linuxtldr

Output:

Changing your existing shell to the Korn shell (KSH)

Log out of your system to reflect the changes, and then execute the following command to verify that the Korn shell (KSH) is your default shell.

$ echo $0

Output:

Verifying the Korn shell (KSH) as the default shell

Congratulations!!! You have Korn Shell (KSH) as the default shell in your Linux system.

Configuring Korn Shell (KSH) in Linux

If you are transitioning from Bash to Korn Shell (KSH), then you might experience that the arrow key (for navigating or going to the previous command), the home key, the end key, and the β€œCtrl+l” shortcut to clear the command from the screen do not work.

It’s kind of frustrating for beginners, but you can easily resolve it by editing the shell configuration file using your choice of text editor, like Vim or Nano.

$ nano ~/.kshrc

#OR

$ vim ~/.kshrc

Then copy/paste the following codes into your shell configuration file.

set -o emacs

keybd_trap () {
  case ${.sh.edchar} in
    $'\f')    .sh.edchar=$'\e\f';;  # clear-screen
    $'\e[1~') .sh.edchar=$'\001';;  # Home = beginning-of-line
    $'\e[4~') .sh.edchar=$'\005';;  # End = end-of-line
    $'\e[5~') .sh.edchar=$'\e>';;   # PgUp = history-previous
    $'\e[6~') .sh.edchar=$'\e<';;   # PgDn = history-next
    $'\e[3~') .sh.edchar=$'\004';;  # Delete = delete-char
  esac
}
trap keybd_trap KEYBD

Output:

Configuring the Korn Shell (KSH)

Save and close the file, and reload the configuration changes using the source command.

$ source ~/.kshrc

Now you can use most of the bash shortcut keys in Korn shell (KSH).

How to Remove the Korn Shell (KSH) in Linux

Lacking so many features that modern interactive shell users require, you might change your mind after using it for a while and wish to go back to your original shell.

If that occurs in your mind, then follow the following steps one by one to remove it completely from your Linux system.

First, change your existing Korn shell (KSH) to your previous shell (considering bash as your previous shell) by executing the following command:

$ sudo chsh -s $(which bash) linuxtldr

Output:

Changing your existing shell to previous shell

After changing, logout and login to your system to reflect the change, and execute the following command to verify your default shell.

$ echo $0

Output:

Verifying the existing shell

Lastly, execute any of the following commands (based on your system type) to remove the Korn shell (KSH) from your system.

$ sudo apt remove ksh                                                                                #On Debian or Ubuntu
$ sudo dnf remove ksh                                                                                #On Fedora or RHEL
$ sudo pacman -R ksh                                                                                 #On Arch or Manjaro

That’s all you require to remove Korn shell (KSH) from your Linux system.

If you want me to write more articles related to Korn Shell (KSH), then do let me know in the comment section; apart from that, I would highly recommend you read this beautiful article or book about KSH.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.