So I found a way to transfer environment variables through ssh without having to change neither server nor client: Transferring environment variable through SSH / quoting in bash/sh/csh/tcsh

But I would also like to be able to transfer bash functions like this one:

$ myfunc() {
> echo Func $1
> }
$ export -f myfunc
$ parallel myfunc ::: a
Func a

Functions are not transferred through ssh:

$ parallel -S server myfunc ::: a
bash: myfunc: command not found
$ parallel --env myfunc -S server myfunc ::: a
bash: line 2: myfunc: command not found

But the content of the function is transferred nicely:

$ parallel --env myfunc -S server echo \$myfunc ::: a
() { echo Func $1} a

So it seems all I need to do now is to somehow tell bash that $myfunc is really the function myfunc.

How can I do that?


That actually proved to be fairly easy (except it does not work in csh). Simply do:

eval myfunc$myfunc

It unfortunately crashes normal variables, to avoid this only do the eval for variables for which the content start with '() {'; that seems to be bash's signature for functions.

  • Today use env_parallel from GNU Parallel. It works for more than functions. – Ole Tange May 4 '17 at 19:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.