Bun Scripting

Topic index · Part of Teach · Glossary

Mission: replace daily bash scripts — file/text transforms, API calls, subprocess orchestration, scheduling — with one toolchain, Bun, whose scripts run identically and type-checked on macOS, Linux, and Windows. Lessons assume you already know JS/TS.

Lessons

Lesson 0001 — Your First Portable Bun Script

Run a .ts script anywhere, read arguments (Bun.argv), and exit with codes & stderr the way bash never guaranteed. Ends by porting one of your own bash scripts.

Lesson 0002 — Read, Transform, Write (File I/O)

Turn "read a file, tweak it, save the result" — the daily sed/grep/awk chore — into a typed Bun script that also composes as a Unix pipe.

Lesson 0003 — Run Programs: Bun.spawn

Replace backticks ($(...)) and $? with a typed helper that runs another program, captures its output, and aborts on failure — the Bun twin of set -e.

Lesson 0004 — The Bun Shell `$`

Write git log pipelines and ls | wc -l-style one-liners in Bun Shell — bash syntax you already know, but cross-platform and immune to injection.

Lesson 0005 — HTTP & API Automation

Turn a curl | jq chore into a typed Bun script: fetch an API, check res.ok, parse JSON, write to disk — secrets from a .env file.

Lesson 0006 — Embedded SQLite with bun:sqlite

Make your scripts remember between runs with bun:sqlite — a real SQL database in one file, no server, and typed queries for stateful daily chores.

Next bun lessons: Bun.Glob batch matching & scheduling. Meanwhile, Lesson 0007 opens the Cloudflare D1 track — it puts Lesson 0006’s SQLite online.

Reference