By default, when we talk about a shell in Linux, most people think of Bash. It is true that it is the most wide spread and arguably one of the simplest shell to begin with, but there are also plenty of other shells out there yearning for your attention. Today, we shall go through another very famous shell: the Korn Shell (“ksh” for the intimates).
At the origin, the Korn Shell was considered to be the commercial alternative to the Bash Shell. Developed in 1983 by David G. Korn for AT&T Bell, the Korn Shell was inspired from the interactivity of the C shell and the effectiveness of the Bourne Shell. It was updated in 1986, then in1988, and was finally released Open Source in 1993. However, before that, since the Korn Shell was a commercial product, a free alternative was created in 1987, called Public Domain Korn Shell. This pdksh that you can still use today is mostly based on the ksh of 1998 and 1993. From today’s perspective, Bash and Korn are pretty much similar and compatible. They are in fact both descending from the same ancestor. However the interaction is slightly different and some of the scripting techniques must be reviewed when going from one to another.
Now that you know more about ksh, let’s install it. For Ubuntu, the installation is fairly simple:
sudo apt-get install ksh
If you are using another distro, you can check out your repositories. In the worst case, download the package from AT&T official download page.
Once the package is installed, you can launch a Korn Shell from any terminal via the command:
If you really like this shell and want it to replace your current one, you can do that by typing
sudo usermod -s /bin/ksh [user name]
As I said before, the Korn Shell is close to the Bash Shell. But before you try anything, you have to know that ksh can imitate the behavior of your favorite text editor. That is the first thing that will make you either like or hate Korn. To be clearer, you can set your shell to have the same shortcuts and functions as a non graphical text editor of your choice. To see a list of which editor are available on your system, use this command from ksh:
For my part, I prefer to use Vi. Therefore, I did
set -o vi
From that point, ksh will behave the same way as vi. And of course, for those of you who know everything about Emacs, do instead
set -o emacs
For the sake of this article, let’s continue with Vi. You now have access to any shortcut and commands that you normally use with Vi. Just remember to use the classic “Esc” key before. For example,
- You can erase a line with “dd”
- Jump to the next word with “e”
- Paste with “p”
- Erase a character with “x”
Let’s go to the interesting part, the auto-completion and the interactive help.
With Bash, you can auto-complete a command by hitting twice the tab key. This will not work in every situation with ksh. Instead, if you setup Vi, you can do “Esc + \”. If you have Emacs, do “Esc + Esc”. And if you wish to see all the possibilities for the current situation, use “Esc + =” with Vi and Emacs. This will display a list of element that you can choose from.
Furthermore, the command history is still accessible. With the Vi interface, you can use the key “k” to go up in the history, and “j” to go down. And move the cursor with the traditional “h” for the left and “l” for the right. From an Emacs perspective, “Ctrl + p” to go up in the history, and “Ctrl + n” to go down. And to move the cursor: “Ctrl + f” for the right, and “Ctrl + b” for the left.
Now let’s talk about scripting. For the lambda user, the Korn Shell resembles the Bash Shell. However, there are more differences about the syntax while writing scripts.
- Replace “#! /bin/bash” with “#! /bin/ksh” (this is pretty obvious, I know)
- The scripts extension should be “.ksh”
- There are no commands “enable” or “declare” anymore
- You can use “print” along with “echo”
- Use “-eq” instead of the traditional “==” in the if statements
- The conditions for loops and if statements must be between double brackets “[[ condition]]”
- All occurrences of “$[expression]” must be replaced by “$((expression))”
- Functions can be declared simply with “function_name()”
- A table can be created via the unique syntax
set -A [table's name] [value 1] [value 2] … [value n]
- The command “read” can be used in a new way
read [variable]?”[message to display]”
You should do some research in order to avoid these minor differences. One great resource is the official FAQ page.
As you can see, Bash and Korn are fairly similar. While the interface can be slightly different, and the scripts must be adapted in some cases, the spirit stays the same. You should not have any trouble for going from one to another. However, if you do, I invite you to consult ksh’s manual page. As a personal challenge, you can test pdksh that I haven’t tried yet, and post your comments here.
I think that’s it for the Korn Shell. Next time we shall explore Zsh Shell, the shell with the most impressive auto-completion feature I’ve seen so far.
What do you think of the Korn Shell? Have you tried another one? Do you have any question concerning the shells? Please let us know in the comments.