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