Debian / Ubuntu Linux: Install KornShell (KSH)

by on January 9, 2011 · 0 comments· LAST UPDATED August 9, 2013

in , ,

I am porting ksh script from Sun/Oracle Unix to Debian / Ubuntu Linux based server hosted on AWS cloud. How do I install ksh (KornShell) in Debian or Ubuntu Linux server? How do I run and test ksh scripts?

Tutorial details
DifficultyEasy (rss)
Root privilegesNo
Requirementsapt-get
Estimated completion time10m
KSH was developed by David Korn at Bell Labs in 1980s. KSH is is quite popular is quite useful automate everyday tasks on Unix or Linux based systems. You can install ksh on Ubuntu or Debian Linux.

Installing KSH on Debian / Ubuntu serer or desktop

Open a terminal and then type the following apt-get command to install the package:
$ sudo apt-get install ksh
OR

Sample outputs:

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
  ksh
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 1,527 kB of archives.
After this operation, 3,191 kB of additional disk space will be used.
Get:1 http://mirrors.service.networklayer.com/ubuntu/ precise/universe ksh amd64 93u-1 [1,527 kB]
Fetched 1,527 kB in 0s (1,988 kB/s)
Selecting previously unselected package ksh.
(Reading database ... 82257 files and directories currently installed.)
Unpacking ksh (from .../archives/ksh_93u-1_amd64.deb) ...
Processing triggers for man-db ...
Setting up ksh (93u-1) ...
update-alternatives: using /bin/ksh93 to provide /bin/ksh (ksh) in auto mode.

Task: Finding ksh location

To find out path to the ksh, type:
$ whereis ksh
OR use the grep command as follows:
$ grep --color ksh /etc/shells
Sample outputs:

Fig.01: Finding ksh path

Fig.01: Finding ksh path

Task: Setting ksh as a default shell

The superuser (root) may change the login shell for any account using any one of the following syntax:
$ sudo chsh -s /bin/ksh UserNameHere
In this example, set default login shell to /bin/ksh for nixcraft user:
$ sudo chsh -s /bin/ksh nixcraft
Regular user can type the following command to change their shell to the ksh:
$ chsh -s /bin/ksh
$ grep nixcraft /etc/passwd

Sample outputs (when prompted enter your own password):

Fig.02: Attempt to change the user's shell to /bin/ksh (/bin/ksh93)

Fig.02: Attempt to change the user's shell to /bin/ksh (/bin/ksh93)


Logout and login again. Verify your shell with the following command:
$ echo $SHELL
Sample outputs:

/bin/ksh93

To see ksh version, type:
$ ksh --version
Sample outputs:

  version         sh (AT&T Research) 93u 2011-02-08

Writing your first ksh script

Create a file called test.ksh using a text editor:

#!/bin/ksh
# Name: test.ksh (Debian/Ubuntu edition)
# Purpose: My first ksh script 
# Author: nixCraft <www.cyberciti.biz> under GPL v2.x+
# ------------------------------------------------------------------------
# set variables 
FILE="/etc/passwd"
NOW="$(date)"
HOSTNAME="`hostname`"
USERS_ACCOUNT="$(wc -l $FILE)"
OS="$(lsb_release -d | awk  '{ print $2 " version " $3}')"
#OS="$(lsb_release -d | awk -F':' '{ print $2 }')"
 
# Greet user
print "Hi, $USER. I'm $0. I'm $SHELL script running on $HOSTNAME at $NOW."
print
print "*** User accounts: $USERS_ACCOUNT"
print "*** Current working directory: $PWD"
print "*** OS: $OS"
 
print "*** Running for loop test just for fun:"
for x in {1..3}
do
    print "Welcome $x times."
done
 

Save and close the file. Run it as follows:

 
chmod +x test.sh
./test.sh
 

Sample outputs:

Fig.03: Running your first KSH script

Fig.03: Running your first KSH script

See also:
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 0 comments… add one now }

Leave a Comment

Tagged as: , , , ,

Previous Faq:

Next Faq: