What Is Kornshell or ksh? – POFTUT

What Is Kornshell or ksh?


Linux and Unix provides different flavors of command line interface. bashis the most popular and use one. But we have alternatives too. kshor Korn Shell is one of them. Kornshell is mainly developed for shell based application development. ksh provides better performance for shell script.

Install Kornshell

We can install Kornshell for most of the Linux, Unix and BSD operating systems. Here are some of them.

Fedora, CentOS, RedHat

$ yum install ksh

Ubuntu, Debian, Kali

$  apt install ksh
Install Ksh Ubuntu, Debian, Kali
Install Ksh Ubuntu, Debian, Kali

Start Kornshell

Starting ksh is very easy. We will just run ksh command in the current shell like below.

$ ksh

Kornshell Script Examples

We can create scripts like in bash. We have to specify the ksh path on the system. In this case we use Ubuntu and the ksh shell is locate at  /usr/bin/ksh. alternative and more clean path is /bin/ksh . We can run this script from bash too because the interpreter line will change current shell to the Korn Shell.

#!/bin/ksh

echo "This is ksh shell script"

Variables

If you have used bash for programming both scripting languages are very similar. Variables can be simply defined with the variable name and related value equation. In this example we will define integer variable named ÀGE which holds 33 .

AGE = 33

For Loop

For loop is very similar to the bashfor loop we can use following example in order to loop though files in the current working directory.

for file in $(ls)
do
   print $file
done

Function Definition

We can create functions by using functionkeyword and the function name we want to use. In this example we will create function named myprint. Function body is surrounded with curly braces.

function myprint
{
   print "This is myprint function"
}

LEARN MORE  Bash Printf Function Tutorial with Examples

Leave a Comment