namespace
$
Runs a shell command with the Bun Shell.
const result = await $`echo "Hello, world!"`.text();
console.log(result); // "Hello, world!"namespace $
class ShellError
An error that occurred while executing a shell command with the Bun Shell.
try { const result = await $`exit 1`; } catch (error) { if (error instanceof $.ShellError) { console.log(error.exitCode); // 1 } }- static stackTraceLimit: number
The
Error.stackTraceLimitproperty specifies the number of stack frames collected by a stack trace (whether generated bynew Error().stackorError.captureStackTrace(obj)).The default value is
10but may be set to any valid JavaScript number. Changes will affect any stack trace captured after the value has been changed.If set to a non-number value, or set to a negative number, stack traces will not capture any frames.
Read from stdout as an ArrayBuffer
@returnsStdout as an ArrayBuffer
const output = await $`echo hello`; console.log(output.arrayBuffer()); // ArrayBuffer { byteLength: 6 }Read from stdout as a Uint8Array
@returnsStdout as a Uint8Array
const output = await $`echo hello`; console.log(output.bytes()); // Uint8Array { byteLength: 6 }Read from stdout as a JSON object
@returnsStdout as a JSON object
const output = await $`echo '{"hello": 123}'`; console.log(output.json()); // { hello: 123 }- @param encoding
The encoding to use when decoding the output
@returnsStdout as a string with the given encoding
Read as UTF-8 string
const output = await $`echo hello`; console.log(output.text()); // "hello\n"Read as base64 string
const output = await $`echo ${atob("hello")}`; console.log(output.text("base64")); // "hello\n" - targetObject: object,constructorOpt?: Function): void;
Create .stack property on a target object
class ShellPromise
A shell command that runs once awaited, or once an output method like
.text()or.json()is called.const myShellPromise = $`echo "Hello, world!"`; const result = await myShellPromise.text(); console.log(result); // "Hello, world!"Read from stdout as an ArrayBuffer
Automatically calls quiet
@returnsA promise that resolves with stdout as an ArrayBuffer
const output = await $`echo hello`.arrayBuffer(); console.log(output); // ArrayBuffer { byteLength: 6 }- onrejected?: null | (reason: any) => TResult | PromiseLike<TResult>
Attaches a callback for only the rejection of the Promise.
@param onrejectedThe callback to execute when the Promise is rejected.
@returnsA Promise for the completion of the callback.
- @param newCwd
The new working directory
- env(newEnv: undefined | Dict<string> | Record<string, undefined | string>): this;
Set environment variables for the shell.
@param newEnvThe new environment variables
const { stdout } = await $`echo $FOO`.env({ ...process.env, FOO: "bun" }); console.log(stdout.toString()); // "bun\n" - onfinally?: null | () => void
Attaches a callback that is invoked when the Promise is settled (fulfilled or rejected). The resolved value cannot be modified from the callback.
@param onfinallyThe callback to execute when the Promise is settled (fulfilled or rejected).
@returnsA Promise for the completion of the callback.
Read from stdout as a JSON object
Automatically calls quiet
@returnsA promise that resolves with stdout as a JSON object
const output = await $`echo '{"hello": 123}'`.json(); console.log(output); // { hello: 123 }Read from stdout as a string, line by line
Automatically calls quiet to disable echoing to stdout.
Configure the shell to not throw an exception on non-zero exit codes. Throwing can be re-enabled with
.throws(true).By default, the shell throws an exception on commands that return non-zero exit codes.
- isQuiet?: boolean): this;
By default, the shell writes to the current process's stdout and stderr while also buffering that output.
quiet()configures the shell to only buffer the output.@param isQuietWhether to suppress output. Defaults to
true - text(encoding?: BufferEncoding): Promise<string>;
Read from stdout as a string.
Automatically calls quiet to disable echoing to stdout.
@param encodingThe encoding to use when decoding the output
@returnsA promise that resolves with stdout as a string
Read as UTF-8 string
const output = await $`echo hello`.text(); console.log(output); // "hello\n"Read as base64 string
const output = await $`echo ${atob("hello")}`.text("base64"); console.log(output); // "hello\n" - onrejected?: null | (reason: any) => TResult2 | PromiseLike<TResult2>): Promise<TResult1 | TResult2>;
Attaches callbacks for the resolution and/or rejection of the Promise.
@param onfulfilledThe callback to execute when the Promise is resolved.
@param onrejectedThe callback to execute when the Promise is rejected.
@returnsA Promise for the completion of which ever callback is executed.
- shouldThrow: boolean): this;
Configure whether the shell should throw an exception on non-zero exit codes.
By default, this is configured to
true. - values: T): Promise<{ [K in string | number | symbol]: Awaited<T[P<P>]> }>;
Creates a Promise that is resolved with an array of results when all of the provided Promises resolve, or rejected when any Promise is rejected.
@param valuesAn array of Promises.
@returnsA new Promise.
- values: Iterable<T | PromiseLike<T>>): Promise<PromiseSettledResult<Awaited<T>>[]>;
Creates a Promise that is resolved with an array of results when all of the provided Promises resolve or reject.
@param valuesAn array of Promises.
@returnsA new Promise.
- values: T): Promise<Awaited<T[number]>>;
The any function returns a promise that is fulfilled by the first given promise to be fulfilled, or rejected with an AggregateError containing an array of rejection reasons if all of the given promises are rejected. It resolves all elements of the passed iterable to promises as it runs this algorithm.
@param valuesAn array or iterable of Promises.
@returnsA new Promise.
- values: T): Promise<Awaited<T[number]>>;
Creates a Promise that is resolved or rejected when any of the provided Promises are resolved or rejected.
@param valuesAn array of Promises.
@returnsA new Promise.
- reason?: any): Promise<T>;
Creates a new rejected promise for the provided reason.
@param reasonThe reason the promise was rejected.
@returnsA new rejected Promise.
- value: T): Promise<Awaited<T>>;
Creates a new resolved promise for the provided value.
@param valueA promise.
@returnsA promise whose internal state matches the provided promise.
- fn: (...args: A) => T | PromiseLike<T>,...args: A): Promise<T>;
Run a function and return a promise of its result. If the function throws, the returned promise rejects with the thrown error.
@param fnThe function to run
@param argsThe arguments to pass to the function. This is similar to
setTimeoutand avoids the extra closure.@returnsA promise that resolves with the function's result
- static withResolvers<T>(): { promise: Promise<T>; reject: (reason?: any) => void; resolve: (value?: T | PromiseLike<T>) => void };
Create a deferred promise with its
resolveandrejectfunctions exposed, so code outside the promise can settle it.const { promise, resolve, reject } = Promise.withResolvers(); setTimeout(() => { resolve("Hello world!"); }, 1000); await promise; // "Hello world!"
interface ShellOutput
Read from stdout as an ArrayBuffer
@returnsStdout as an ArrayBuffer
const output = await $`echo hello`; console.log(output.arrayBuffer()); // ArrayBuffer { byteLength: 6 }Read from stdout as a Uint8Array
@returnsStdout as a Uint8Array
const output = await $`echo hello`; console.log(output.bytes()); // Uint8Array { byteLength: 6 }Read from stdout as a JSON object
@returnsStdout as a JSON object
const output = await $`echo '{"hello": 123}'`; console.log(output.json()); // { hello: 123 }- @param encoding
The encoding to use when decoding the output
@returnsStdout as a string with the given encoding
Read as UTF-8 string
const output = await $`echo hello`; console.log(output.text()); // "hello\n"Read as base64 string
const output = await $`echo ${atob("hello")}`; console.log(output.text("base64")); // "hello\n"
- @param pattern
Brace pattern to expand
const result = braces('index.{js,jsx,ts,tsx}'); console.log(result) // ['index.js', 'index.jsx', 'index.ts', 'index.tsx'] - newEnv?: Dict<string> | Record<string, undefined | string>
Change the default environment variables for shells created by this instance.
@param newEnvDefault environment variables to use for shells created by this instance
import {$} from 'bun'; $.env({ BUN: "bun" }); await $`echo $BUN`; // "bun"