0

In PowerShell I can pipe output into a for-each loop like so:

 C:> ls C:\ | % { $_.Length / 1024 }

Note that ls is an alias for Get-ChildItem and % is an alias for ForEach-Object.

Is there a similar thing in Bash that affords quick iteration over output, in a simple one-liner?

It raises the question of what constitutes an item and its boundaries, so I was assuming line breaks would be the obvious design. PowerShell is sort of object-oriented so it's not a problem.

Otherwise, I see for used in Bash scripts; can I enter a multi-line statement at the prompt?

Use Case

I'm new to Bash and wanted to run git rm --cached for each line printed by find . -name ".DS_Store" but I'd rather a general answer that teaches me to fish than a specific solution to that problem :)

  • The dupes will teach you to fish in various orders of specifity - https://unix.stackexchange.com/questions/377979/how-to-make-a-for-loop-in-command-line for for loops in one line, https://unix.stackexchange.com/questions/7558/execute-a-command-once-per-line-of-piped-input for running commands for each line of some input in general and https://unix.stackexchange.com/questions/25921/how-can-i-run-a-specific-command-for-each-find-result for your specific use case – muru Nov 24 '21 at 00:31
  • Thank you very much. I did look first, I promise. I have a theory that it's easier to find answers online when you already know the solution. Cheers,. – Luke Puplett Nov 25 '21 at 08:37

0 Answers0