exported functions shell scripting

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: <[email protected]>

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




Increase ad revenue 50-250% with Ezoic


More Articles by

Find me on Google+

© Tony Lawrence



Kerio Samepage


Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

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





I wanted to learn how to swim, so Google showed me how to turn on the water at the sink and let me splash it around a bit. They then dragged me into a helicopter, flew way out into the ocean and dumped me out. (Tony Lawrence)

Legend has it that every new technology is first used for something related to sex or *redacted*. That seems to be the way of humankind. (Tim Berners-Lee)







This post tagged: