Bash scripts break on spaces in filenames. Node's child_process is verbose. Bun Shell gives you shell scripting in JavaScript — safe, cross-platform, and actually readable.
What Is Bun Shell?
Bun Shell (Bun.$) is a shell-like API built into Bun. It combines the convenience of shell scripting with the safety of JavaScript.
import { $ } from "bun"
// Run shell commands
await $`echo "Hello from Bun Shell!"`
// Capture output
const result = await $`ls -la`.text()
console.log(result)
// Pipe commands
await $`cat data.json | jq '.users[]'`
// Template literals are safe (auto-escaped!)
const filename = "file with spaces.txt"
await $`cat ${filename}` // Works! No quoting issues
// Environment variables
await $`echo $HOME`
Safe by Default
// Bash: user input can break everything
// rm -rf $user_input ← if user_input is "/ --no-preserve-root", disaster
// Bun Shell: template variables are auto-escaped
const userInput = "/ --no-preserve-root"
await $`echo ${userInput}` // Prints literally, doesn't execute
Practical Scripts
// Deploy script
const branch = await $`git branch --show-current`.text()
console.log(\`Deploying \${branch.trim()}...\`)
await $`npm run build`
await $`docker build -t myapp:latest .`
await $`docker push myapp:latest`
console.log("Deployed!")
// File processing
import { $ } from "bun"
import { Glob } from "bun"
const glob = new Glob("**/*.ts")
for await (const file of glob.scan(".")) {
const lines = await $`wc -l ${file}`.text()
console.log(lines.trim())
}
// Error handling
try {
await $`curl https://api.example.com/health`.quiet()
} catch (e) {
console.error(\`Health check failed: \${e.exitCode}\`)
await $`systemctl restart myapp`
}
Why Bun Shell Over Bash
- Cross-platform — works on macOS, Linux, Windows (same script!)
- Safe — no word splitting, no glob expansion on variables
- JavaScript — real error handling, async/await, npm packages
- Fast — Bun starts in <10ms
- Readable — template literals vs cryptic bash syntax
Building developer tools? Check out my automation toolkit or email spinov001@gmail.com.
Top comments (0)