APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

exported functions shell scripting

© December 2004 Tony Lawrence

Most shells support "exported functions". For example, in the Bourne shell, you use "export -f", and ksh is "typeset -xf". But these are confusing:

If you define foo within your .kshrc (or whatever $ENV points at), the function is available at the command line to any invocation of ksh.

If you then mark it for export (typeset -xf foo), it becomes available to scripts, but it STILL not available to scripts beginning with #!/bin/ksh.

It is THAT part that is the source of the mind-boggling confusion: you can obviously see that as new ksh has the function, yet it won't work in a script that begins with #!/bin/ksh . The script does not have the function, as can easily be seen by putting "functions" into such a script: functions listed in /.kshrc will not be there.

However- if you both define AND mark the function for export within the $ENV file, it then IS available to those scripts.

# kshrc
function foo 
echo "Hi there!"

typeset -xf foo

The reason these exported functions aren't usually available to scripts beginning with #! is because the kernel doesn't propogate them when it execs the script. Scripts without #! are forked, and the function would be passed.

Message-ID: <77gt84$6uv$1@remarQ.com>

discipline functions

A Kornshell "discipline" function ties a function to a variable, effectively creating a dynamic variable. In the example given below, $date becomes a variable that calls "date" when referenced.

$ function date.get { .sh.value=$(date); }
$ echo $date

You define the function using the desired variable name with the special ".get", ".set" or ".unset" appended. The special "sh.value" is assigned within the function; sh.name and sh.subscript are also defined. These pass their results back to your "variable.get".

More examples are shown at Examples of using "discipline" functions.

Bash does not have these discipline functions, probably because "echo $(date)" does the same thing with only a few more keystrokes.

Got something to add? Send me email.

(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> exported functions shell scripting ––>Re: SCO shellscripts

Inexpensive and informative Apple related e-books:

iOS 10: A Take Control Crash Course

Take Control of Numbers

Sierra: A Take Control Crash Course

Digital Sharing Crash Course

Take Control of Parallels Desktop 12

More Articles by © Tony Lawrence

Printer Friendly Version

Have you tried Searching this site?

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us

Printer Friendly Version

Several people have told me that my inability to suffer fools gladly is one of my main weaknesses. ((Edsger W. Dijkstra)

Linux posts

Troubleshooting posts

This post tagged:



Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode