Skip to main content
Provided better explanation about how the code works.
Source Link

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,

  • $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 $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.

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 "$@"' _

The function will get all the arguments passed to the bash command, but starting form 0, so, inside the function $0 would be _ and $1 would be next argument. See the link in the second comment.

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.

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 "$@"' _

The function will get all the arguments passed to the bash command, but starting form 0, so, inside the function $0 would be _ and $1 would be next argument. See the link in the second comment.

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 "$@"' _

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 "$@"' _

The function will get all the arguments passed to the bash command, but starting form 0, so, inside the function $0 would be _ and $1 would be next argument. See the link in the second comment.

Source Link
cuonglm
  • 158.1k
  • 41
  • 342
  • 420

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 "$@"' _