Bun Shell makes shell scripting with JavaScript and TypeScript ergonomic and safe. It is a cross-platform bash-like shell with seamless JavaScript interop — no extra dependencies required.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/oven-sh/bun/llms.txt
Use this file to discover all available pages before exploring further.
Features
Cross-platform
Works on Windows, Linux, and macOS. Common commands like
ls, cd, rm are implemented natively — no cross-env or rimraf needed.Safe by default
All interpolated values are treated as literal strings, preventing shell injection attacks.
JavaScript interop
Use
Response, Blob, ArrayBuffer, Bun.file(), and other JS objects directly as stdin/stdout.Familiar syntax
Supports redirection, pipes, globs, environment variables, and command substitution just like bash.
Basic usage
Run commands using the$ tagged template literal. By default, output goes to stdout:
Capturing output
Use.text() to capture stdout as a string:
.quiet() to suppress output without capturing it:
Capturing stdout and stderr as buffers
Awaiting a shell command directly returns an object withstdout and stderr as Buffers:
Other output formats
Error handling
By default, commands that exit with a non-zero code throw aShellError:
Disabling throws
Use.nothrow() on a single command to check exitCode manually instead of catching:
$.nothrow() or $.throws(false) to change the default behavior for all commands:
Piping
Pipe the output of one command to another using|, just like in bash:
Redirection
Bun Shell supports all standard bash redirection operators, as well as redirecting to and from JavaScript objects.| Operator | Description |
|---|---|
< | Redirect stdin |
> or 1> | Redirect stdout (overwrite) |
2> | Redirect stderr (overwrite) |
&> | Redirect stdout and stderr |
>> | Redirect stdout (append) |
2>> | Redirect stderr (append) |
2>&1 | Redirect stderr to stdout |
1>&2 | Redirect stdout to stderr |
Redirect to a JavaScript object
Buffer, typed arrays, ArrayBuffer, SharedArrayBuffer, Bun.file(path), Bun.file(fd).
Redirect from a JavaScript object
Buffer, typed arrays, ArrayBuffer, SharedArrayBuffer, Bun.file(), Response.
File redirection examples
Environment variables
Inline env vars
Set environment variables inline just like in bash:Per-command env with .env()
Override environment variables for a single command:
Global env with $.env()
Set default environment variables for all commands:
Working directory
Per-command .cwd()
Global $.cwd()
Command substitution
Use$(...) to insert the output of one command into another:
Use the
$(...) syntax for command substitution. Due to how Bun uses the raw property on template literals, the backtick syntax (`...`) does not work for nested command substitution.Builtin commands
Bun Shell ships native implementations of common commands for cross-platform compatibility:cd, ls, rm, echo, pwd, bun, cat, touch, mkdir, which, mv, exit, true, false, yes, seq, dirname, basename
Any command not in this list is looked up in the system PATH.
Running .sh scripts
Use Bun to run.sh files directly. Bun Shell interprets them cross-platform:
script.sh
Utilities
$.braces — brace expansion
$.escape — escape strings
{ raw: '...' }:
Security
Bun Shell does not invoke a system shell like/bin/sh. It is a re-implementation that treats all interpolated variables as single, literal strings, preventing command injection: