Vaclav Kosar's face photo

Vaclav Kosar

Vaclav Kosar is a senior software developer. This blog is focused on interesting technology, tips and news.

Functional ForEach In Bash

28 Jul 2016

Don’t you hate verbosity of Bash’s while-do statements when writing in-line scripts? No worries, you can improve on that!

Instead of writing long-winded

while IFS= read l; do <<<command>>>; done

you can save space and time using a function. But how to allow flexible use of the line that was read? Functional programmers will recognize familiar foreach command in below:

foreach () { while IFS= read -r l; do eval "[email protected]"; done; }

Notice usage of the evil “eval” function. The eval function is important here to allow flexible usage of the variable “l”. To stay secure always use parenthesis around the variable reference e.g.:

cat files.txt | foreach 'mv "$l" "${l/.txt/.md}"';

Above should keep all evils at bay.

Try it out and let me know below!

Report any trackers to [email protected].