Skip to main content
Bun includes a built-in, cross-platform shell that you can use directly in JavaScript and TypeScript files. It uses the $ 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 directory
  • echo - Print text
  • ls - List files
  • cat - Concatenate files
  • rm - Remove files
  • mkdir - Create directories
  • mv - Move files
  • cp - Copy files
  • pwd - Print working directory
  • which - Locate commands
  • exit - Exit with code
These work identically on Windows, macOS, and Linux.

Advanced Features

Glob Patterns

Command Substitution

Process Substitution

Running Scripts

Inline Scripts

Script Files

Create executable shell scripts with Bun:
Make it executable:

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:
  • ReadableStream instead of Unix pipes
  • TextDecoder for encoding
  • fetch() for HTTP

Comparison with Other Tools

vs. Node.js child_process

vs. Zx (Google)

Bun’s shell is similar to zx but:
  • Built-in (no extra dependency)
  • Faster (no subprocess for simple commands)
  • Cross-platform (works on Windows natively)

Best Practices

  1. Use template literals for readability
  2. Check exit codes for critical commands
  3. Stream large outputs
  4. Use quiet mode for noisy commands