38

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?

1

2 Answers 2

46

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 "$@"' _
10
  • 5
    One more question, why is the trailing _ required? Is it to stop xargs from processing anything else?
    – GMaster
    Oct 1, 2014 at 6:51
  • 4
    @FazleA.: read unix.stackexchange.com/questions/152391/…
    – cuonglm
    Oct 1, 2014 at 7:08
  • 2
    I am getting "export: Illegal option -f" when I do export -f Nov 22, 2018 at 6:35
  • 2
    Why does this work? Or, more specifically, why is the bash -c subshell needed (why can't xargs just detect and use the function just defined in the same shell process)?
    – Magnus
    Mar 13, 2021 at 22:26
  • 1
    @Magnus because xargs run the command in new process, not with the original one. That's why you need export f to make the function visible in sub-shells.
    – cuonglm
    Mar 15, 2021 at 2:25
1

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
2
  • 1
    For zsh try: env_parallel instead of parallel.
    – Ole Tange
    Dec 6, 2021 at 17:05
  • 2
    Thanks @OleTange . And also special thanks for writing the GNU parallel tool
    – GMaster
    Dec 9, 2021 at 16:58

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.