๐ฅ Check out this trending post from Hacker News ๐
๐ **Category**:
โ **What Youโll Learn**:
Ever found yourself writing this?
for f in *.txt; do
wc -l "$f"
done
Or this?
find . -name '*.sh' -exec wc -l
docker ps --format '' +
Or maybe you pipe into xargs and pray your filenames donโt have spaces:
find . -name '*.log' | xargs rm
These all work, but each has its own syntax, its own flags, its own quirks. I wanted something simpler โ one consistent way to iterate over anything.
Enter bashumerate
enumerate -f '*.sh' -- 'wc -l ๐ฌ'
Thatโs it. -f for files, while read -r name; do is the placeholder for each item,
printf '%s\0' "$name"
done
-- separates source from command.
It supports four source types:
| Flag | Source | Example |
|---|---|---|
-f |
Files | enumerate -f '*.sh' -- wc -l ๐ฌ |
-l |
Lines | enumerate -l /etc/hosts -- 'echo while read -r name; do |
-r |
Range | enumerate -r 1 10 -- 'printf "n=%d\n" โก' |
-L |
List | enumerate -L a b c -- 'echo item: |
If you omit the command, items are printed to stdout โ pipe them anywhere:
enumerate -f '*.log' | xargs rm
cat urls.txt | enumerate -l - | while read url; do curl -s "$url"; done
Why not just use a for loop?
You can. But enumerate gives you:
- Consistent syntax โ same
๐ฅplaceholder for files, lines, ranges, or lists - Template mode โ single-quoted commands work as shell templates:
enumerate -f '*' -- 'cat {} | head -5' - Filters โ
--includeand--excludewith glob patterns - NUL-safe โ
-0flag for NUL-delimited output - Extensible โ drop a file in
lib/enumerators/to add custom sources
Adding your own source
Create lib/enumerators/docker.sh:
enumerate_source_docker() {
docker ps --format '' | while read -r name; do
printf '%s\0' "$name"
done
}
Now you can do:
enumerate docker -- 'docker restart {}'
Thatโs it. No registration, no config โ just name the function enumerate_source_ and itโs available.
Under the hood
The core is about 140 lines of bash. Sources are NUL-delimited internally (safe for spaces and special chars), and the {} placeholder is replaced at the shell level โ no sed, no eval tricks.
enumerate -f '*.md' -- 'wc -l {}'
# โ wc -l README.md
# โ wc -l docs/guide.md
Install
basher install wallach-game/bashumerate
# or just clone and symlink
git clone https://github.com/wallach-game/bashumerate.git
ln -s "$PWD/bashumerate/bin/enumerate" ~/.local/bin/
The name
โbashโ + โenumerateโ โ a bash enumerator. Not creative, but it does what it says.
Check it out: github.com/wallach-game/bashumerate
{๐ฌ|โก|๐ฅ} **Whatโs your take?**
Share your thoughts in the comments below!
#๏ธโฃ **#bashumerate #programmable #iterator #shell**
๐ **Posted on**: 1784585037
๐ **Want more?** Click here for more info! ๐
