Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use xargs with a function in fish shell?

Tags:

function

fish

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 image 957
Sérgio Mucciaccia Avatar asked Dec 23 '22 06:12

Sérgio Mucciaccia


1 Answers

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 somecommands 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.

like image 192
faho Avatar answered Jan 15 '23 04:01

faho