Tag Archives: function

How do I use xargs with a function in bash?

Question: How do I use xargs with a function that I’ve defined in bash?

Answer:

The following example uses the export and runs in a new process which is why the export f switch is needed.


doEcho() {
  echo $1
}

export -f doEcho
echo You echo | xargs -d' ' -t -n1 -P2 bash -c 'doEcho "$@"' _

Note: This function works in bash and will not work in zsh.