KSH: Import File With Variables and Functions

In Korn shell (KSH), you can import variables and functions from a file using the . (dot) command. The . command reads the contents of a file and executes it as if it were part of the current shell.

Here’s an example of how you can use the . command to import variables and functions from a file:

File my_vars.sh:

#!/bin/ksh

# Define variables
var1="Hello"
var2="World"

# Define a function
my_func() {
echo "This is my function."
}

In your main script:

#!/bin/ksh

# Import variables and functions from my_vars.sh
. ./my_vars.sh

# Use the imported variables
echo $var1 $var2

# Call the imported function
my_func

When you run the main script, it will import the variables and functions from my_vars.sh and then use them as if they were defined in the main script. The output should be:

Hello World
This is my function.

Note that the file that you are importing must be a valid KSH script. The . command will not work with binary files or files in other formats.

Leave a Comment