I am trying to use xargs to call a more complex function in parallel.
#!/bin/bash
echo_var(){
echo $1
return 0
}
seq -f "n%04g" 1 100 |xargs -n 1 -P 10 -i echo_var {}
exit 0
This returns the error
xargs: echo_var: No such file or directory
Any ideas on how I can use xargs to accomplish this, or any other solution(s) would be welcome.
Exporting the function should do it (untested):
You can use the builtin
printfinstead of the externalseq:Also, using
return 0andexit 0like that masks any error value that might be produced by the command preceding it. Also, if there’s no error, it’s the default and thus somewhat redundant.@phobic mentions that the Bash command could be simplified to
moving the
{}directly inside it. But it’s vulnerable to command injection as pointed out by @Sasha.Here is an example why you should not use the embedded format:
Another example of why not:
This is what is output using the safe format:
This is comparable to using parameterized SQL queries to avoid injection.
I’m using
datein a command substitution or in escaped quotes here instead of thermcommand used in Sasha’s comment since it’s non-destructive.