CentOS / RHEL Install KornShell (KSH) Command

See all Sun CentOS Linux related FAQs
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?

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).
Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements Linux terminal
Category Package Manager
OS compatibility AlmaLinux Amazon Linux CentOS Fedora RHEL Rocky Stream
Est. reading time 3 minutes
Advertisement

Steps to install ksh in RHEL or CentOS Linux

  1. Open the Terminal app.
  2. Type the ‘sudo yum install ksh‘ command on CentOS/RHEL.
  3. Type the ‘sudo dnf install ksh‘ command on Fedora Linux.
  4. Update your shell in the /etc/passwd using the chsh command
  5. Start using your ksh shell.

Let us see steps in detailed to install KornShell (KSH) on a RHEL/CentOS Linux based system.

How to install the KornShell (KSH) in CentOS / RHEL

Open a terminal and then type the following yum command to install the ksh shell:
$ sudo yum install ksh
OR
# 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
================================================================================
Installing:
 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 
 
Installed:
  ksh.x86_64 0:20100621-19.el6_4.4                                              
 
Complete!

A note about Fedora Linux users

Type the following dnf command to install ksh under Fedora Linux:
$ sudo dnf install ksh

How can I find out path for ksh shell?

To find out path to the ksh use the which command/whereis command or type command as follows:
$ which ksh
$ type ksh
$ whereis ksh

OR you can use the grep command as follows:
$ grep --color ksh /etc/shells

Fig.01: Finding ksh path

Fig.01: Finding ksh path

The /bin/ksh is now added to list of acceptable shells, verify it with the cat command:
$ cat /etc/shells
Sample outputs:

/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh

How do I set ksh as a default shell in a CentOS or RHEL system?

The superuser (root) may change the login shell for any account using any one of the following syntax. Try the chsh command:
$ sudo chsh -s /bin/ksh UserNameHere
OR
# 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 echo command or printf command:
$ echo "$SHELL"
Sample outputs:

/bin/ksh

See my blog post “How do I find out what shell I am using on Linux/Unix?” for more info. Do you want to see the ksh version too? Pass the --version parameter as follows:
$ ksh --version
Here is what I see from my CentOS Linux version 6.x/7.x:

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

Sample ksh program for testing purpose (optional)

Create a file called test.ksh using a text editor such as nano command or vim command/emacs command. For example:

#!/bin/ksh
# Name: test.ksh
# 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)"
 
# 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 "*** Running for loop test just for fun:"
for x in {1..3}
do
    print "Welcome $x times."
done
Save and close the file by pressing ESC+:wq+[Enter] when using vim as text editor. Run your .sh or .ksh script it as follows:
$ chmod +x test.sh
$ ./test.sh

Fig.02 Ksh script in action.

Fig.02 Ksh script in action.

Summing up

And that’s it! KSH has been installed and tested on a CentOS or RHEL-based system. Have a great time KSH scripting!

See also

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

2 comments… add one
  • aref ghobadi Aug 16, 2015 @ 10:31

    Hi,
    thanks a lot

  • davi Sep 25, 2015 @ 12:59

    Very Good! tks

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.