Exporting a function in shell

Please tell me how to export a function in parent shell (bash, sh or ksh) so that the function will be available to all the child process launced from the parent process?


Solution 1:

The export -f feature is specific to Bash:

parent

#!/bin/bash
plus1 () { echo $(($1 + 1)); }
echo $(plus1 8)
export -f plus1
./child 14 21

child

#!/bin/bash
echo $(plus1 $(($1 * $2)) )

Solution 2:

In sh, it is not possible to export a function, as noted by Charles Duffy.

Solution 3:

If you are using ksh or zsh:

You can use the environment variable FPATH, wherein you can place all your functions.

If FPATH is set on an interactive interpreter, and a command or function is not found in the current shell environment or the PATH, the directories listed there are searched for the existence of a file named after the missing command. If one is found, it is sourced in the current shell environment, and expected to define the function.

So, you can place all your functions in a location in FPATH, and child scripts will also be able to find it.

You can use the autoload command in shell scripts to load the functions you require:

autoload fun_a fun_b

In zsh, autoload is required for FPATH to work. In ksh and its close relatives, I believe it simply causes functions defined in FPATH to override regular command in your PATH, as they would if defined directly.

Some details on FPATH and autoload:

  • http://docstore.mik.ua/orelly/unix3/upt/ch29_13.htm
  • http://users.speakeasy.net/~arkay/216-7.4KshFunctions.html

Solution 4:

Functions are not exportable by nature. However you can export strings, so I have a little trick here:

func="$(typeset -f funcname)"
export func

To import the function, re-define it from the exported string:

# in subshell
eval "$func"

Solution 5:

If you create subshells with ( ) then they will inherit a snapshot of all the definitions, parameters, and shell variables.

If you execute them like programs then you can put the definitions in .bashrc.

If you are trying to spoof an existing script into executing a wrapper or substitution for a PATH command, then .bashrc will work depending on details of the execution. If not, you could instead execute a wrapper script that simply does a . or source of an include file that defines the functions and then does the same thing with the shell script with the commands to be substituted.

The wrapper script might look something like:

script=$1
shift
. include.sh
. $script "$@"

The idea is that the first parameter is the name of the real script and remaining parameters are the args, then the above script is run instead.