≡ Menu

KSH: Import File With Variables and Functions

How do I import variables and functions defined in lib.ksh script to another ksh script called setup.ksh under Linux / Unix like operating systems? How do I execute commands from a file in the current KSH shell?

To read and execute commands from given FILENAME in the current shell use the following syntax:

. /path/to/file


. /path/to/lib.ksh

The . (dot) is one of the KSH built-in commands. You can also use an alias called source as follows:

source /path/to/lib.ksh


Create a script called /tmp/lib.ksh as follows:

# defaul values ##
# Display info
function showvech {
  printf "Vehicle: %s\n" $vech
  printf "Type (ac or non-ac): %s\n" $type
  printf "Rent (per/km): Rs.%d km\n" $rent
# Set info
function setvech {

Create a script called test.ksh:

# source our /tmp/lib.ksh
. /tmp/lib.ksh
# show defaults
# set new values and display it back
setvech "Jeep" 9 "Non-A/C"

Sample outputs:

Vehicle: Bus
Type (ac or non-ac): A/C
Rent (per/km): Rs.14 km
Vehicle: Jeep
Type (ac or non-ac): Non-A/C
Rent (per/km): Rs.9 km
Share this tutorial on:

Your support makes a big difference:
I have a small favor to ask. More people are reading the nixCraft. Many of you block advertising which is your right, and advertising revenues are not sufficient to cover my operating costs. So you can see why I need to ask for your help. The nixCraft, takes a lot of my time and hard work to produce. If you use nixCraft, who likes it, helps me with donations:
Become a Supporter →    Make a contribution via Paypal/Bitcoin →   

Don't Miss Any Linux and Unix Tips

Get nixCraft in your inbox. It's free:

{ 0 comments… add one }
Security: Are you a robot or human?

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">

   Tagged with: , , , , , , ,