Try exporting functionthe 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 function will get all the arguments passed to the bashbash command are, but starting form 0well, so
passed into the bash environment, but starting from 0.
So, inside the function,
$0is “_” and$1is “This”$0is “_” and$1is “is”$0is “_” and$1is “a”- ︙
See Bash -c with positional parameters.
Note that $0export -f would beworks only in Bash,
and -Pn (_--max-procs=max-procs and $1 would be next argument. See the link)
works only in the second comment GNU xargs.