Skip to content
TrySitePrice

KSH: Import File With Variables and Functions

Best/Tutorial

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
nvar1="Hello"
nvar2="World"

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

In your main script:

#!/bin/ksh
# Import variables and functions from my_vars.sh
n. ./my_vars.sh
# Use the imported variables
echo $var1 $var2

# Call the imported function
nmy_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
nThis 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.

n (Alprazolam)