Our computing machines are endowed with myriad goodly powers, but in the Beginning they were made for one thing: to loop. O, and also to count. Two things, then: to loop and to count. And to process, of course. These three things, then, they were made for: to loop, to count, and to process. And to be programmable, all right, that as well. Amongst the things they were made for, are such diverse elements as . . . I’ll come in again.
Verily they have their loops in High Level Languages, in their Rubie or Pythonne or JavaScripte or C-Flat. But this profoundly pow’rful construct is also readily available on the Unicks line of command, whereupon it is wondrous useful. There are sundry ways to achieve said looping, first at hand today is the shell built-in ‘for’, eff oh arr.
The incantation spans multiple Lines and has some subtleties and wiles, but it is not over complicated. The first Line contains the keyword itself, then a variable Name of thy choosing, then the keyword, ‘in’, and finally the list of Items to be subsequently regarded, each in turn. Then the ‘enter’ key, at which point ye shall behold the secondary shell prompt, the right-facing battle pennant: ‘>’. Type then the single keyword, ‘do’, and press ‘enter’ once more.
Now thou art ready to enter the commands which will act upon each of your Items. It is not required to indent these commands, though I feel it proper. End each command by pressing ‘enter’ again. When thy commands are concluded, add one last Line with the final keyword ‘done’. Id est:
Leaving aside the ‘echo’ command in this Example, one might wonder if this may have been accomplished instead with a single, simple ‘cat’ command and clever use of wyldcards. Alack, the order of the Directory names would have been governed by the divine principle of Alphabeticality — Fourth, then Second, then Third — which was not desired in this case.
It is less common to thus list static Items for ‘for’ to iterate, and rather more common to employ the Globbing of Filenames. As before, the advantage of ‘for’ over one-liners with wyldcards is that multiple statements may be issued therein. Id est:
The back-tick execution in that Example brings us to a third and wholly wonderful way to use this looping device. For that execution may itself be used to generate the list of Items that ‘for’ will dutifully peruse, one by one. The possibilities are as limitless as the herds of Unicorns which crowd the countryside and ravage our crops; here is but one advantageous example:
As shewn in these Examples, the list of Items provided for ‘for’ to iterate o’er may be divided by any white-space, be it space, or tabbe, or newline.
Ctrl-D, Amen.