在 shell 中导出函数

请告诉我如何在父 shell (bash、 sh 或 ksh)中导出一个函数,以便该函数可用于从父进程启动的所有子进程?

56612 次浏览

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.

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

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:

declare -x -f NAME

More info

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

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

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"