Exporting a function in shell

FunctionShellExport

Function Problem Overview


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?

Function Solutions


Solution 1 - Function

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

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

Solution 3 - Function

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 4 - Function

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:

Solution 5 - Function

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.

Solution 6 - Function

declare -x -f NAME

More info

-f        restrict action or display to function names and definitions
-x        to make NAMEs export

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSachin ChourasiyaView Question on Stackoverflow
Solution 1 - FunctionDennis WilliamsonView Answer on Stackoverflow
Solution 2 - FunctionanolView Answer on Stackoverflow
Solution 3 - FunctioniBugView Answer on Stackoverflow
Solution 4 - FunctionVenkataramesh KommojuView Answer on Stackoverflow
Solution 5 - FunctionDigitalRossView Answer on Stackoverflow
Solution 6 - FunctionZomboView Answer on Stackoverflow