How To Write First UNIX Korn Shell Script Program

Can you tell me how to write a UNIX korn shell script program? I’m new to UNIX scripting and HP-UX UNIX operating system.

Korn shell scripting under UNIX / Linux can be used to automate lots of stuff. It easy to write a shell script. You must know how to use a text editor such as vi to write a script.

Writing your first Korn shell script

Let us write a shell program to print knowledge is power on screen. Type the following command to open file:
vi hello.ksh
The first line should be as follows:


It is called a shebang. It consists of a number sign and an exclamation point character (#!), followed by the full path to the interpreter such as /bin/ksh. All scripts under UNIX execute using the interpreter specified on a first line.
Next append code as follows:

# A shell script to print message
# Written by Tom - Jan/13/2008
print "Knowledge is power"

Save and close the file. At the end your script should look like as follows:

# A shell script to print message
# Written by Tom - Jan/13/2008
# ------------------------------
print "Knowledge is power"

Set executable permission

Type the following command to set executable permission:
chmod +x hello.ksh

Run your korn shell script

Type the following command:
Sample output:

Knowledge is power

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 14 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
14 comments… add one
  • Scott Dec 8, 2008 @ 21:06

    great job on youe xplainations. I enjoy your site

  • Suhail Jun 16, 2009 @ 4:57

    Its a good intro and i had successfully wrote my first script, but its better to have a vi or any other editor tutorial mix with this, i first learn the vi and then this one, and it works.

    Anyways a goood one. Thanks once again.

  • suraj bhagat Sep 23, 2009 @ 3:35

    please give me some programs which r follows.

    1)write a shell script program take two strings & check that the string are same or not

    2) sript program to take marks of five subject of a students with name & display the grade in which he is passed .

    3) enter a character from a user & check that itbis a lower case letter, upper case letter, digit, special char.

    4)write a program to find the string is start with ovel, digita, or ends with digit, ovel & the word contains of five letters.

    5)create a file student.dat which contain details of student as rool no.,name,want to be, on plattform, birthdate.

  • Bill Watson Mar 1, 2011 @ 0:40

    I am new to this program and I do not know where to begain. This is my directions and yes I am s student trying to learn this. and no I am not looking for you to do it for me just help me along.

  • Bill Watson Mar 1, 2011 @ 0:41

    Write a Unix shell script (one program) called Project1_lastname (your lastname). This script will do the following tasks:

    Using “if” statement checks for a file called “student.txt” in the current directory. Removes (deletes) the file if it exist. This file will collect last name, first name, and grade for 5 students.

    Use a while loop to get the information (last name, first name, and grade) for 5 students. Grades should be 0-100. Use three different variable names (fname, lname, and grade) to read the data from the keyboard.

    Store the data read from the keyboard in a file called “student.txt”. After reading all the data for 5 students, display contents of student.txt file on the monitor.

    • shiva Dec 5, 2011 @ 11:46

      i want something more apart from this.

  • sandeep Jul 10, 2011 @ 15:42

    thanks a lot

  • eavera Mar 3, 2012 @ 8:40

    actually i installed unix &vi editor on my pc i know to open & save the file
    but i dont know to execute the shell program
    for example if i typed like
    raed a
    echo $a
    ~in vi
    what command should i use to execute ‘a’?
    where it is executed in vi itself or in unix?

  • shilpa Mar 12, 2012 @ 14:30

    Please help me to write a shell program to create a function mkcd() which would create all the directories present in the path supplied ti its argument and change over to the last directories in this path thus, $mkcd d1/d2/d3/d4/d5 sholud be create the path the five directories and change the present working directories to d5?

  • priya May 30, 2012 @ 4:12

    shell program to compute the power of “x”

  • Saurav Bhattacharyya Jul 22, 2012 @ 6:44

    Hi ,

    I have a problem which needs to be solved by Unix shell-scripting(awk scripting is also allowed).

    Input File:
    3,M.Tech,IIT Guwahati

    The Output File would be like this:-
    1,3,M.Tech,IIT Guwahati

    i.e. ,, in a single line for each student.
    For e.g.,Student No 1 has 2 qualifications, B.Sc from Calcutta
    and M.Sc from Stanford.

    Please suggest a time-efficient(as this needs to be simulated for crores of records) & brilliant way of doing this.I will highly appreciate any help from you.

    Waiting for your kind help…

    • Z. May 8, 2013 @ 1:10

      awk -F ‘,’ ‘{ print $1 }’ file.txt | uniq -c | while read a b; do echo “${a},${b},$(grep ^${b} test | cut -d ‘,’ -f 2- | paste -s -d ‘,’ -)”; done

  • Kekgabile Gone Madibela Apr 25, 2014 @ 10:42

    Write a shell program for the below actions using case construct
    a)Display the menu below
    List_ _ _ _1
    Display content _ _ _ _ _ _ _ 2
    Copy file _ _ _ _ _ _ _ _ _ 3
    b)Based on the selected value the program should execute. For eg. If the user enter 1, then display the files and directories. If the user enter 2, it should accept the file name and display its content. If the user enter 3, it should accept 2 file names and copy the file1 to file2.
    thank you in advance!!

  • Lisa Aug 20, 2014 @ 19:31

    Yaay! I wrote my first Korn Shell Script Program. Great instructions!

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum