AI summary by TwinMind
Bash? Nah, I Have Bun.
Theo - t3․gg
12m 38s
Bun Shell Introduction
- Bun now competing with Bash through new shell implementation
- JavaScript as world's most popular scripting language but difficult to run shell scripts in JS
- Node.js original goal included quick scripting but became focused on large server applications
- Bun shell = new experimental embedded language/interpreter for cross-platform shell scripts in JS/TS
Cross-Platform Shell Problems
- Existing shells don't work well in JavaScript for multiple reasons:
- macOS zsh different from Linux bash and Windows command
- Different syntaxes, commands, flags, behaviors per platform
- Scripts promised to work often fail between platforms
- Performance flag issues between Linux bash vs macOS
- Cross-platform scripting actually very difficult
- Single file with code that runs properly everywhere not achievable with traditional shells
NPM Polyfill Dependencies
- Community relies on polyfills for missing commands
- Rimraf package: 60 million downloads/week just for rmrf on Windows
- Environment variables don't work on Windows: 65 million/week package downloads
- Windows = lesser child, only non-Unix platform (Unix/Linux/Mac all similar cores)
Shell Performance Issues
- Shells take too long to start
- Alpine Linux x64: ~7ms to launch new shell
- Starting shell can take longer than running command itself
- For loops with multiple commands = expensive 7ms blocks unless parallelized externally
Bun Shell Implementation
- Not spawning native shell like child processes
- Bun wrote their own shell scripting language
- Uses dollar sign syntax:
await $ls *.js`` - Template string literals for function calls with sanitized arguments
- All template variables escaped for security
- Example:
fuji.js;rm -rfbecomes single quoted argument, prevents command injection
JavaScript Integration Features
- Redirect stdout to JavaScript buffers:
pipe(buffer) - Redirect stdout to files using
file()function - Standard pipe syntax works:
| - Can pipe response objects as stdin
- Built-in commands: cd, echo, rm, globbing, environment variables, redirection, piping
- FFmpeg workflows much easier with buffer piping
Platform Support & Use Cases
- Works on macOS, Windows, Linux
- Drop-in replacement for simple shell scripts in Bun
- Powers package.json scripts in
bun runfor Windows - Real use cases: build scripts, CI workflows
- Universal abstraction layer for Windows/Mac/Linux compatibility
Demo: jQuery + Bun Shell Integration
- Combined jQuery, jsdom, Bun shell, JQ (JSON parser)
$$for jQuery (avoiding conflict with Bun's$)- jsdom for window object in server environment
- Ajax call → JSON response → echo/pipe to JQ → extract name field
- Multiple technologies: jQuery, jsdom, Bun, JavaScript, Bash, JQ
