LinuxCommandLibrary

bun-exec

Execute package scripts or binaries

TLDR

Run a simple command

$ bun exec "echo hello"
copy

Run a command with flags
$ bun exec "ls -la"
copy

Run a command containing quotes
$ bun exec "echo \"hello friends\""
copy

Run a combined shell command
$ bun exec "mkdir test && cd test"
copy

Run a script file
$ bun exec [path/to/script]
copy

SYNOPSIS

bun exec [OPTIONS] <command> [ARGS...]
bun exec [OPTIONS] -- <command> [ARGS...]
bun exec [script-name]

PARAMETERS

<command>
    The command or script to be executed. If it matches a package.json script name, that script will be run. Otherwise, it will attempt to find the command in the PATH, including node_modules/.bin.

[ARGS...]
    Additional arguments to be passed directly to the executed <command>.

--
    A special separator. All arguments following -- will be treated as arguments for the executed <command> and will not be parsed by bun exec itself. This is useful when your command's arguments might conflict with bun exec's own options.

--cwd <path>
    Set the current working directory for the executed command to the specified <path>.

--env <key=value>
    Set an environment variable for the executed command. This option can be specified multiple times to set several variables.

--silent
    Suppress Bun's internal logging output, showing only the output from the executed command.

--help
    Display a help message for bun exec.

DESCRIPTION

bun exec is a powerful subcommand of the Bun JavaScript runtime, designed to execute arbitrary shell commands or scripts. Its primary function is to provide a fast and convenient way to run executables installed locally within your project's node_modules/.bin directory, similar to how npx or npm exec operates in the Node.js ecosystem.

When invoked, bun exec sets up an environment where project-specific binaries are readily available in the PATH, allowing you to run tools like ESLint, TypeScript compiler (tsc), or local test runners without needing to specify their full path. It can also be used to directly execute scripts defined in your package.json file, provided the command matches a script name. This command is crucial for managing and executing development tools and project-specific tasks efficiently within a Bun-powered workflow.

CAVEATS

bun exec is a subcommand of the Bun JavaScript runtime, not a standalone Linux utility named 'bun-exec'. It requires Bun to be installed on your system. Its behavior relies on the presence of node_modules/.bin and entries in your package.json 'scripts' section. If Bun is not installed or not in your system's PATH, the command will not be found.

RUNNING <I>PACKAGE.JSON</I> SCRIPTS

If the command you pass to bun exec matches a script name defined in your package.json (e.g., "start": "node index.js"), bun exec will automatically run that script. This provides a flexible way to invoke project-specific tasks.

EXECUTING LOCAL BINARIES

One of the most common uses for bun exec is to run executables installed in your project's node_modules/.bin directory. For example, after installing eslint, you can simply run bun exec eslint . to execute it without needing to specify ./node_modules/.bin/eslint.

HISTORY

The bun runtime, developed by Jarred Sumner, was publicly released in 2022, rapidly gaining traction for its speed and all-in-one tooling. bun exec was introduced as a core part of Bun's CLI to provide a native, high-performance alternative to existing Node.js ecosystem tools like npx and npm exec. Its design focuses on seamless integration with the Bun module resolver and package manager, aiming for minimal overhead when executing local project binaries and scripts.

SEE ALSO

bun(1), bun-run(1), npx(1) (Node.js ecosystem), npm-exec(1) (Node.js ecosystem), bash(1)

Copied to clipboard