How To Write First UNIX Korn Shell Script Program

by Vivek Gite [Last updated: September 10, 2008]

Q. 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.

A. 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:

#!/bin/ksh

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:

#!/bin/ksh
# 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:
./hello.ksh
Sample output:

Knowledge is power
Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 1 comment… read it below or add one }

1 Scott 12.08.08 at 9:06 pm

great job on youe xplainations. I enjoy your site

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tagged as: , , , , , , , , , , , , ,

Previous post: FreeBSD Install BASH Shell

Next post: Linux Virtual Network Interfaces Go Down When I bring Down eth0:0