43 lines
907 B
Plaintext
43 lines
907 B
Plaintext
---
|
|
title: Run a Shell Command
|
|
sidebarTitle: Run shell command
|
|
mode: center
|
|
---
|
|
|
|
Bun Shell is a cross-platform bash-like shell built into Bun.
|
|
|
|
It runs shell commands from JavaScript and TypeScript. To get started, import the `$` function from the `bun` package.
|
|
|
|
```ts foo.ts icon="/icons/typescript.svg"
|
|
import { $ } from "bun";
|
|
|
|
await $`echo Hello, world!`; // => "Hello, world!"
|
|
```
|
|
|
|
---
|
|
|
|
The `$` function is a tagged template literal that runs the command and returns a promise that resolves with the command's output.
|
|
|
|
```ts foo.ts icon="/icons/typescript.svg"
|
|
import { $ } from "bun";
|
|
|
|
const output = await $`ls -l`.text();
|
|
console.log(output);
|
|
```
|
|
|
|
---
|
|
|
|
To iterate over each line of the output, use the `lines` method.
|
|
|
|
```ts foo.ts icon="/icons/typescript.svg"
|
|
import { $ } from "bun";
|
|
|
|
for await (const line of $`ls -l`.lines()) {
|
|
console.log(line);
|
|
}
|
|
```
|
|
|
|
---
|
|
|
|
See [Bun Shell](/runtime/shell).
|