Can I "export" functions in bash?

In Bash you can export function definitions to sub-shell with

export -f function_name

For example you can try this simple example:

./script1:

#!/bin/bash

myfun() {
    echo "Hello!"
}

export -f myfun
./script2

./script2:

#!/bin/bash

myfun

Then if you call ./script1 you will see the output Hello!.


"Exporting" a function using export -f creates an environment variable with the function body. Consider this example:

$ fn(){ echo \'\"\ \ \$; }
$ export -f fn
$ sh -c printenv\ fn
() {  echo \'\"\ \ \$
}

This means that only the shell (just Bash?) will be able to accept the function. You could also set the function yourself as the Bash only considers envvars starting with () { as function:

$ fn2='() { echo Hi;}' sh -c fn2
Hi
$ fn3='() {' sh -c :
sh: fn3: line 1: syntax error: unexpected end of file
sh: error importing function definition for `fn3'

If you need to "export" this variable over SSH, then you really need the function as a string. This can be done with the print option (-p) for functions (-f) of the declare built-in:

$ declare -pf fn
fn () 
{ 
    echo \'\"\ \ \$
}

This is very useful if you have more complex code that needs to be executed over SSH. Consider the following fictitious script:

#!/bin/bash
remote_main() {
   local dest="$HOME/destination"

   tar xzv -C "$dest"
   chgrp -R www-data "$dest"
   # Ensure that newly written files have the 'www-data' group too
   find "$dest" -type d -exec chmod g+s {} \;
}
tar cz files/ | ssh user@host "$(declare -pf remote_main); remote_main"

Building on @Lekensteyn's answer...

If you use declare -pf it will output all the previously defined functions in the current shell to STDOUT.

At that point you can redirect STDOUT to wherever you want and in effect stuff the previously defined functions wherever you want.

The following answer will stuff them into a variable. Then we echo that variable plus the invocation of the function that we want to run into the new shell that is spawned as a new user. We do this by using sudo with the -u (aka. user) switch and simply running Bash (which will receive the piped STDOUT as the input to run).

As we know that we are going from a Bash shell to a Bash shell we know that Bash will interpret the previous shells defined functions correctly. The syntax should be fine as long as we are moving between one Bash shell of the same version to a new Bash shell of the same version.

YMMV if you are moving between different shells or between systems that may have different versions of Bash.

#!/bin/bash
foo() {
  echo "hello from `whoami`"
}

FUNCTIONS=`declare -pf`; echo "$FUNCTIONS ; foo" | sudo -u otheruser bash
# $./test.sh
# hello from otheruser

Tags:

Function

Bash