How can I use zsh's zargs with list of commands?

Viewed 110

How can I use zsh's zargs with list of commands? I am aware of using something like sh -c but it seems like zsh would be able to do something more efficient.

I thought perhaps an anonymous function like:

zargs -I arg -- **/*.git -- function { cd $1/.. ; git log -1 --format=%cd } arg

but I get

zsh: parse error near `}'

Note: I am also aware I can solve this example with

zargs -I arg -- **/.git -- git -C arg/.. log -1 --format=%cd

but I'm looking for the more general solution.

1 Answers

zargs is just a shell function, which can only take arguments, it's not a keyword in the syntax of that shell like for/while...

Here, you may as well use a loop:

for dir (**/.git(N/)) (cd -- $dir:h && git log -1 --format=%cd)

With zargs, you could use a function which you define beforehand:

log() (
  cd -- $1:h && git log -1 --format=%cd
)
zargs -I arg -- **/.git(/) -- log arg

If you want to inline the code in a generic way, you could define a helper like:

run() eval "shift; $1"
zargs -I arg -- **/.git(/) -- run '(cd -- $1:h && git log -1 --format=%cd)' arg

zargs is useful to break down long list of arguments to avoid the "arg list too long" limitation of execve(), but for usages like with -I arg here, I find it has little advantage over a for loop.

Related