Working with lists of items in the Shell

Another useful idiom for command-line junkies to learn is

list-of-something | do-something-with-each-item

This can be expressed in a number of different ways. My go-to way is the following:

list-of-something | while read f;do do-something-with-each-item $f;done;

The real beauty is this can be built up incrementally, testing out the commands until one has got it right.

For example, to confirm the syntax of a the do-something-with-each-item, I prefix it with an echo to see what command is going to be run i.e.

list-of-something | while read f;do echo do-something-with-each-item $f;done;

If the list-of-something is very large or to confirm that I have the right subset of the list, I add some filters i.e.

list-of-something | grep something | head | while read f;do echo do-something-with-each-item $f;done;

The benefit of this incremental/iterative way of building up a command is that it is quick – up-arrow, edit the command and enter to try again.

The keystrokes to master and memorise are

while read f;do $f;done;

These are then embellished by adding a list-of-something | before the while, and an echo do-something-with-item before the $f.