bashumerate โ€” A programmable iterator for your shell

๐Ÿ’ฅ 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
printf '%s\0' "$name"
done
is the placeholder for each item, -- 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
printf '%s\0' "$name"
done
'
-r Range enumerate -r 1 10 -- 'printf "n=%d\n" โšก'
-L List enumerate -L a b c -- 'echo item:
docker ps --format '' '

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 โ€” --include and --exclude with glob patterns
  • NUL-safe โ€” -0 flag 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! ๐ŸŒŸ

By

Leave a Reply

Your email address will not be published. Required fields are marked *