GNU Parallel and Bash functions: How to run the simple example from the manual
Since version 20160722 you can instead use env_parallel
:
doit() { echo "$@"; }
echo world | env_parallel doit Hello
You just need to activate env_parallel
by adding it to .bashrc
. You can add it to .bashrc
by running this once:
env_parallel --install
You cannot call a shell function from outside the shell where it was defined. A shell function is a concept inside the shell. The parallel
command itself has no way to access it.
Calling export -f doit
in bash exports the function via the environment so that it is picked up by child processes. But only bash understands bash functions. A (grand)*child bash process can call it, but not other programs, for example not other shells.
Going by the message “Command not found”, it appears that your preferred shell is (t)csh. You need to tell parallel
to invoke bash instead. parallel
invokes the shell indicated by the SHELL
environment variable¹, so set it to point to bash.
export SHELL=$(type -p bash)
doit () { … }
export -f doit
parallel doit ::: 1 2 3
If you only want to set SHELL
for the execution of the parallel
command and not for the rest of the script:
doit () { … }
export -f doit
SHELL=$(type -p bash) parallel doit ::: 1 2 3
I'm not sure how to deal with remote jobs, you may need to pass --env=SHELL
in addition to --env=doit
(note that this assumes that the path to bash
is the same everywhere).
And yes, this oddity should be mentioned more prominently in the manual. There's a brief note in the description of the command
argument, but it isn't very explicit (it should explain that the command
words are concatenated with a space as a separator and then passed to $SHELL -c
), and SHELL
isn't even listed in the environment variables section. (I encourage you to report this as a bug; I'm not doing it because I hardly ever use this program.)
¹ which is bad design, since SHELL
is supposed to indicate a user interface preference for an interactive command line shell, and not to change the behavior of programs.