This is my code


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?


Try exporting 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 "$@"' _
  • Thanks, but what do you think about my answer above? – GMaster Oct 1 '14 at 6:16
  • @FazleA.: I intend to comment on that, but because you ask here, I will answer here. That won't work, since when you didn't pass any parameter to showword. – cuonglm Oct 1 '14 at 6:18
  • 2
    One more question, why is the trailing _ required? Is it to stop xargs from processing anything else? – GMaster Oct 1 '14 at 6:51
  • 4
    @FazleA.: read unix.stackexchange.com/questions/152391/… – cuonglm Oct 1 '14 at 7:08
  • 1
    I am getting "export: Illegal option -f" when I do export -f – Sagar Chamling Nov 22 '18 at 6:35

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.