≡ Menu

CentOS / RHEL: Install KornShell (KSH)

I am porting ksh script from Sun/Oracle Unix to Linux. How do I install ksh (KornShell) in CentOS / Fedora / Red Hat Enterprise Linux? How do I run and test ksh script on RHEL/CentOS Linux?

Tutorial details
DifficultyEasy (rss)
Root privilegesNo
Estimated completion time10m
KSH was developed by David Korn at Bell Labs in 1980s. KSH is is quite popular is quite loved by sysadmins to automate everyday tasks on Unix like operating systems. You can install ksh on CentOS / RHEL. KSH-93 is the most recent version of the KornShell. It is a shell programming language, which is upward compatible with "sh" (the Bourne Shell).

How to install the Korn Shell (KSH)

Open a terminal and then type the following yum command to install the ksh shell:
$ sudo yum install ksh
# yum install ksh
Sample outputs:

Loaded plugins: auto-update-debuginfo, protectbase, rhnplugin, security
This system is receiving updates from RHN Classic or RHN Satellite.
0 packages excluded due to repository protections
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package ksh.x86_64 0:20100621-19.el6_4.4 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
 Package  Arch        Version                   Repository                 Size
 ksh      x86_64      20100621-19.el6_4.4       rhel-x86_64-server-6      687 k
Transaction Summary
Install       1 Package(s)
Total download size: 687 k
Installed size: 0
Is this ok [y/N]: y
Downloading Packages:
ksh-20100621-19.el6_4.4.x86_64.rpm                       | 687 kB     00:00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : ksh-20100621-19.el6_4.4.x86_64                               1/1
  Verifying  : ksh-20100621-19.el6_4.4.x86_64                               1/1
  ksh.x86_64 0:20100621-19.el6_4.4

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

How do I set 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
# chsh -s /bin/ksh UserNameHere
In this example, set default login shell to /bin/ksh for nixcraft user:
# chsh -s /bin/ksh nixcraft
Regular user can type the following command to change their shell to the ksh:
$ chsh -s /bin/ksh
Logout and login again. Verify your shell with the following command:
$ echo $SHELL
Sample outputs:


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

  version         sh (AT&T Research) 93t+ 2010-06-21

Sample ksh program

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

# Name: test.ksh
# Purpose: My first ksh script
# Author: nixCraft <www.cyberciti.biz> under GPL v2.x+
# ------------------------------------------------------------------------
# set variables 
# Greet user
print "Hi, $USER. I'm $0. I'm $SHELL script running on $HOSTNAME at $NOW."
print "*** User accounts: $USERS_ACCOUNT"
print "*** Current working directory: $PWD"
print "*** Running for loop test just for fun:"
for x in {1..3}
    print "Welcome $x times."

Save and close the file. Run it as follows:

chmod +x test.sh

Sample outputs:

Fig.02 Ksh script in action.

Fig.02 Ksh script in action.

See also:
Tweet itFacebook itGoogle+ itPDF itFound an error/typo on this page?

{ 2 comments… add one }

  • aref ghobadi August 16, 2015, 10:31 am

    thanks a lot

  • davi September 25, 2015, 12:59 pm

    Very Good! tks

Leave a Comment