HeadlinesBriefing favicon HeadlinesBriefing.com

Bashumerate: A Universal Shell Iterator

Hacker News •
×

The author developed `bashumerate`, a programmable iterator for the shell, to simplify common tasks previously handled by `for` loops, `find -exec`, or `xargs`. Frustrated by the inconsistent syntax and quirks of these methods, especially with filenames containing spaces, the goal was to create a single, consistent way to iterate over various data sources.

`bashumerate` offers a unified command structure, using `enumerate -f '*.sh' -- 'wc -l {}'` as an example. It supports four main source types: files (`-f`), lines (`-l`), ranges (`-r`), and lists (`-L`), with `{}` serving as a placeholder for each iterated item. If no command is provided, items are printed to standard output, allowing for piping to other commands. The tool boasts a consistent syntax, template mode for commands, filtering options (`--include`, `--exclude`), NUL-safe handling (`-0`), and extensibility for custom sources.

Custom sources can be added by creating shell functions named `enumerate_source_<name>`. The core logic is approximately 140 lines of bash, handling data internally with NUL delimiters for safety and replacing the `{}` placeholder at the shell level. Installation is straightforward via `basher` or by cloning the repository. The name `bashumerate` is a straightforward combination of "bash" and "enumerate."