How to iterate over all lines displayed by a command in zsh?

How to iterate over all lines output by zsh without installing IFS?

The reason is because I want to run a command for each file output by the command, and some of these files contain spaces.

For example, given the remote file:

foo/bar baz/gamma

That is, one directory "foo" containing the subdirectory "bar baz" containing the file "gamma".

Then runs:

git ls-files --deleted | xargs ls

A report will open in this file, which will be processed as two files: 'foo / bar' and '/ baz / gamma'.

I need it to treat it as a single file: foo / bar baz / gamma.

+5
source share
2 answers

tr -0 xargs, , \000 (NUL), - , NUL , :

git ls-files --deleted | tr '\n' '\000' | xargs -0 ls

: foo/bar baz/gamma\n foo/bar baz/gamma\000, xargs -0 ,

+4

:

ls "${(@f)$(git ls-files --deleted)}"

f . (@s:||:) , ||. @ . , , IFS , .

, :

git ls-filed --deleted | while IFS= read -r line; do ls $line; done

, , zargs.

autoload -U zargs
zargs -- "${(@f)$(git ls-files --deleted)}" -- ls
+7

All Articles