I need to use xargs to call a function in parallel in fish shell. I tried this:
#!/bin/fish
function func
echo $argv
end
echo 'example1' 'example2' | xargs -n1 func
But I got this:
xargs: func: No such file or directory
So, how can I make it work?
Using bash this worked:
#!/bin/bash
function func {
echo $1
}
export -f func
echo 'example1' 'example2' | xargs -n1 bash -c 'func $@' _
Like Kurtis said, xargs won't work with functions. It can be made to work by launching another shell, but that's a hack.
What would probably work better for you is to not use xargs at all.
Instead of
echo 'example' | xargs func
just run func example
.
In general, fish's command substitutions should be used for this.
So instead of
somecommand | xargs func
use
func (somecommand)
this will split somecommand
s output on newlines, which is strictly speaking more strict than xargs
(which will split on "blanks" and newline by default, but it will allow shell-like quoting and escaping), which is typically what you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With