Tools: Breaking: Bun Shell Has a Free API You've Never Heard Of
What Makes Bun Shell Special?
The Hidden API: Shell-JavaScript Bridge
Advanced Piping API
Error Handling
Build Scripts Made Easy
Quick Start
Why Developers Love Bun Shell Bun Shell ($) is a cross-platform shell built into Bun that lets you run shell commands directly in JavaScript/TypeScript. It's not just a child_process wrapper — it's a full shell with pipes, redirects, and globbing that works identically on macOS, Linux, and Windows. A DevOps engineer shared: "We replaced 15 bash scripts with Bun Shell. Same functionality, but now it works on our Windows CI runner too. And we get TypeScript autocomplete for the JavaScript parts." Need automation scripts or data tools? Email [email protected] or check my automation toolkit. Using Bun? What do you think of the shell API? Templates let you quickly answer FAQs or store snippets for re-use. Hide child comments as well For further actions, you may consider blocking this person and/or reporting abuse
$ import { $ } from 'bun'; // Run shell commands with template literals
const result = await $`echo hello world`;
console.log(result.text()); // 'hello world\n' // Pipe shell output to JavaScript
const files = await $`ls -la src/`.text();
const tsFiles = files.split('\n').filter(f => f.endsWith('.ts')); // Pipe JavaScript to shell
const data = JSON.stringify({ name: 'test', value: 42 });
await $`echo ${data} | jq .name`; // Chain commands
await $`cat package.json | jq .dependencies | sort`;
import { $ } from 'bun'; // Run shell commands with template literals
const result = await $`echo hello world`;
console.log(result.text()); // 'hello world\n' // Pipe shell output to JavaScript
const files = await $`ls -la src/`.text();
const tsFiles = files.split('\n').filter(f => f.endsWith('.ts')); // Pipe JavaScript to shell
const data = JSON.stringify({ name: 'test', value: 42 });
await $`echo ${data} | jq .name`; // Chain commands
await $`cat package.json | jq .dependencies | sort`;
import { $ } from 'bun'; // Run shell commands with template literals
const result = await $`echo hello world`;
console.log(result.text()); // 'hello world\n' // Pipe shell output to JavaScript
const files = await $`ls -la src/`.text();
const tsFiles = files.split('\n').filter(f => f.endsWith('.ts')); // Pipe JavaScript to shell
const data = JSON.stringify({ name: 'test', value: 42 });
await $`echo ${data} | jq .name`; // Chain commands
await $`cat package.json | jq .dependencies | sort`;
// Pipe between shell and Response objects
const resp = await fetch('https://api.github.com/repos/oven-sh/bun');
await $`echo ${resp} | jq .stargazers_count`; // Write to files
await $`echo "Hello" > output.txt`;
await $`cat input.txt >> output.txt`; // Glob support
const count = await $`wc -l src/**/*.ts`.text();
console.log('Total lines:', count);
// Pipe between shell and Response objects
const resp = await fetch('https://api.github.com/repos/oven-sh/bun');
await $`echo ${resp} | jq .stargazers_count`; // Write to files
await $`echo "Hello" > output.txt`;
await $`cat input.txt >> output.txt`; // Glob support
const count = await $`wc -l src/**/*.ts`.text();
console.log('Total lines:', count);
// Pipe between shell and Response objects
const resp = await fetch('https://api.github.com/repos/oven-sh/bun');
await $`echo ${resp} | jq .stargazers_count`; // Write to files
await $`echo "Hello" > output.txt`;
await $`cat input.txt >> output.txt`; // Glob support
const count = await $`wc -l src/**/*.ts`.text();
console.log('Total lines:', count);
import { $ } from 'bun'; try { await $`exit 1`;
} catch (err) { console.log('Exit code:', err.exitCode); console.log('stderr:', err.stderr.toString());
} // Or use .nothrow() to handle errors yourself
const result = await $`might-fail`.nothrow();
if (result.exitCode !== 0) { console.log('Failed:', result.stderr.toString());
}
import { $ } from 'bun'; try { await $`exit 1`;
} catch (err) { console.log('Exit code:', err.exitCode); console.log('stderr:', err.stderr.toString());
} // Or use .nothrow() to handle errors yourself
const result = await $`might-fail`.nothrow();
if (result.exitCode !== 0) { console.log('Failed:', result.stderr.toString());
}
import { $ } from 'bun'; try { await $`exit 1`;
} catch (err) { console.log('Exit code:', err.exitCode); console.log('stderr:', err.stderr.toString());
} // Or use .nothrow() to handle errors yourself
const result = await $`might-fail`.nothrow();
if (result.exitCode !== 0) { console.log('Failed:', result.stderr.toString());
}
// build.ts — cross-platform build script
import { $ } from 'bun'; await $`rm -rf dist`;
await $`mkdir -p dist`;
await $`bun build src/index.ts --outdir dist --minify`;
await $`cp package.json dist/`;
await $`echo "Build complete!"`; // Run tests in parallel
await Promise.all([ $`bun test src/utils/`, $`bun test src/api/`, $`bun test src/components/`,
]);
// build.ts — cross-platform build script
import { $ } from 'bun'; await $`rm -rf dist`;
await $`mkdir -p dist`;
await $`bun build src/index.ts --outdir dist --minify`;
await $`cp package.json dist/`;
await $`echo "Build complete!"`; // Run tests in parallel
await Promise.all([ $`bun test src/utils/`, $`bun test src/api/`, $`bun test src/components/`,
]);
// build.ts — cross-platform build script
import { $ } from 'bun'; await $`rm -rf dist`;
await $`mkdir -p dist`;
await $`bun build src/index.ts --outdir dist --minify`;
await $`cp package.json dist/`;
await $`echo "Build complete!"`; // Run tests in parallel
await Promise.all([ $`bun test src/utils/`, $`bun test src/api/`, $`bun test src/components/`,
]);
# Install Bun
-weight: 500;">curl -fsSL https://bun.sh/-weight: 500;">install | bash # Use $ in any .ts file
bun run build.ts
# Install Bun
-weight: 500;">curl -fsSL https://bun.sh/-weight: 500;">install | bash # Use $ in any .ts file
bun run build.ts
# Install Bun
-weight: 500;">curl -fsSL https://bun.sh/-weight: 500;">install | bash # Use $ in any .ts file
bun run build.ts - Cross-platform — same scripts work on macOS, Linux, Windows
- Template literals — write shell commands as tagged templates
- JavaScript interop — pipe between JS and shell seamlessly
- Built-in — no -weight: 500;">npm -weight: 500;">install, comes with Bun