46

This is my code

#!/bin/bash

showword() {
  echo $1
}

echo This is a sample message | xargs -d' ' -t -n1 -P2 showword

So I have a function showword which echoes whatever string you pass as a parameter to the function.

Then I have xargs trying to call the function and pass one word at a time to the function, and run 2 copies of the function in parallel. The thing that is not working is xargs doesn't recognize the function. How can I achieve what I am trying to do, how can I make xargs work with the function showword?

GMaster
  • 5,992
  • 3
  • 28
  • 32

2 Answers2

49

Try exporting the function, then calling it in a subshell:

showword() {
  echo $1
}

export -f showword
echo This is a sample message | xargs -d' ' -t -n1 -P2 bash -c 'showword "$@"' _

This causes xargs to execute

bash -c 'showword "$@"' _ This
bash -c 'showword "$@"' _ is
bash -c 'showword "$@"' _ a
            ︙

The arguments passed to the bash command are, well, passed into the bash environment, but starting from 0.  So, inside the function,

  • $0 is “_” and $1 is “This
  • $0 is “_” and $1 is “is
  • $0 is “_” and $1 is “a
  •       ︙

See Bash -c with positional parameters.

Note that export -f works only in Bash, and -Pn (--max-procs=max-procs) works only in GNU xargs.

cuonglm
  • 150,973
  • 38
  • 327
  • 406
2

Just adding an alternative solution using parallel which I have started using in place of xargs. The task is much easier with parallel

#!/bin/bash

showword() {
  echo $1
}
export -f showword

parallel -j2 showword {} ::: This is a sample message
  • -j2 makes sure 2 copies of the function is run in parallel
  • ::: anything after this is passed as separate arguments to parallel, separation is whitespace
  • {} is replaced by the argument passed into parallel and fed into the showword function

If you are using zsh shell this solution will not work since zsh does not have any feature to export functions. You will need something like this:

#!/usr/bin/zsh

showword() {
  echo $1
}

# add the following to your .zshrc if you want env_parallel in your shell permanently
source /usr/bin/env_parallel.zsh

env_parallel -j2 --env showword showword {} ::: This is a sample message
GMaster
  • 5,992
  • 3
  • 28
  • 32
  • Where does `/usr/bin/env_parallel.zsh` come from? I don't have it on my system... – friederbluemle Oct 19 '22 at 21:18
  • 1
    @friederbluemle On Ubuntu, if I run the command `dpkg -S /usr/bin/env_parallel.zsh`, it says the file `/usr/bin/env_parallel.zsh` comes from the package `parallel` – GMaster Oct 20 '22 at 02:55