Translate

Archives

Source a Function in KSH93

Ksh93 allows you to source a file or a function into local scope. Other shells support sourcing a file but ksh93 is the only shell that I am aware of that allows you to also invoke a function with the source command.

From the KSH93 manpage:

. name [ arg ... ]
    If name is a function defined with the  function  name  reserved
    word syntax, the function is executed in the current environment
    (as if it had been defined with the name()  syntax.)   Otherwise
    if  name  refers to a file, the file is read in its entirety and
    the commands are executed in the current shell environment.  The
    search path specified by PATH is used to find the directory con‐
    taining the file.  If any arguments arg are given,  they  become
    the  positional  parameters  while processing the .  command and
    the original positional parameters are restored upon completion.
    Otherwise  the  positional  parameters  are unchanged.  The exit
    status is the exit status of the last command executed.


Here is a simple shell script which demonstrates the functionality:

#!/bin/ksh

{
   printf "Function testme invokedn"
   var=testme
}

. testme
echo $var

exit 0

Comments are closed.