Exporting a function in shell

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"

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


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)) )