Exporting a function in shell

Sachin Chourasiya picture Sachin Chourasiya · Dec 11, 2009 · Viewed 49k times · Source

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?

Answer

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