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 :)

New contributor
Luke Puplett is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Browse other questions tagged or ask your own question.