Search code examples
linuxbashmercurialcygwin

How to invoke a command for each result?


I want to invoke a command for each result of the executed command. Particularly, I want to invoke hg diff <filename> for each result, returned by hg status. Is it possible to do with a single bash command?

For instance, hg status returned:

M project/pom.xml
M project/api/pom.xml

And I want to execute hg diff for both project/pom.xml and project/api/pom.xml. How can I do that?


Solution

  • If you want to see all modified files, you could just run hg diff without arguments.

    More generally, you could use something like

    hg status -mn0 | xargs -0 hg diff
    

    Here hg status -mn0 prints the file names of modified files delimited by a null byte, which is a better delimiter than newline because file names can contain newlines, and pipes the whole thing through xargs -0 hg diff, which splits its input at the null bytes and calls hg diff for each token. This can be used for all sorts of commands, not just hg diff.