Search code examples
shellforeachzshglob

Can I execute the command for each result of the file globbing in zsh without for?


I am searching for away to execute the current command for each result of the file globbing without building a for loop. I saw this somewhere but can't remember where exactly.

(The echo is just an example, it should also work with psql for example)

Example:

$ touch aaa bbb ccc ddd
$ echo "--- " [a-c]*
---  aaa bbb ccc

Desired output:

---  aaa
---  bbb
---  ccc

Kown way:

$ for i in [a-c]*; do echo "--- " $i ; done
---  aaa
---  bbb
---  ccc

Could be done using for. But maybe there is a way to do it shorter? Maybe like using double curly braces around the glob or whatever?

Thanks. :)


Solution

  • You may be looking for zargs, a command with the same purpose as xargs but a saner interface.

    autoload -U zargs
    zargs -n 1 -- * -- mycommand