$ template literal for running shell commands.
Basic Usage
Template Interpolation
Safe Variable Substitution
Variables are automatically escaped:Raw Strings
Use.raw to pass values without escaping:
Piping and Redirection
Pipes
Connect commands with pipes:Input/Output Redirection
Here Documents
Control Flow
Conditional Execution
Command Grouping
Working with Output
Text Output
JSON Output
Binary Data
Line-by-Line
Streaming
Error Handling
Checking Exit Codes
Exceptions
By default, commands don’t throw on non-zero exit:Try/Catch
Environment Variables
Setting Variables
Accessing Variables
Options
Change Directory
Quiet Mode
Suppress stderr output:Nothrow
Prevent throwing on errors:Built-in Commands
Bun’s shell includes cross-platform implementations of common commands:cd- Change directoryecho- Print textls- List filescat- Concatenate filesrm- Remove filesmkdir- Create directoriesmv- Move filescp- Copy filespwd- Print working directorywhich- Locate commandsexit- Exit with code
Advanced Features
Glob Patterns
Command Substitution
Process Substitution
Running Scripts
Inline Scripts
Script Files
Create executable shell scripts with Bun:Performance
Bun’s shell is optimized for:- Fast startup - No subprocess overhead for built-in commands
- Memory efficiency - Streams data instead of buffering
- Concurrent execution - Multiple commands run in parallel where possible
Differences from Bash
Not a Full Shell
Bun’s shell supports common scripting patterns but is not POSIX-compliant:- No functions or aliases
- Limited job control
- Simplified parameter expansion
Web Standards
Bun uses Web APIs:ReadableStreaminstead of Unix pipesTextDecoderfor encodingfetch()for HTTP
Comparison with Other Tools
vs. Node.js child_process
vs. Zx (Google)
Bun’s shell is similar tozx but:
- Built-in (no extra dependency)
- Faster (no subprocess for simple commands)
- Cross-platform (works on Windows natively)
Best Practices
-
Use template literals for readability
-
Check exit codes for critical commands
-
Stream large outputs
-
Use quiet mode for noisy commands