# Bash

URL: https://skywake.dev/docs/agents/bash

skywake/bash gives an agent a bash shell over a disk branch, with rg, grep and find answered by the API.

Agents already know bash. `skywake/bash`, part of the `skywake` package, gives them a shell whose files are a branch of a disk, built on [just-bash](https://github.com/vercel-labs/just-bash). It runs in your process, with no container and no network: `ls`, `cat`, `sed`, `awk`, `jq`, `head`, `wc`, `sort`, `mkdir`, `cp`, `mv` and `rm` work on the branch's files directly.

`rg`, `grep -r` and `find` are answered by the API instead of reading the tree file by file, so they stay fast on a branch with millions of files.

```bash norun
npm install skywake
```

## Run commands [#run-commands]

`shell(disk)` opens a shell on the branch the handle is on. `run(command)` returns `stdout`, `stderr` and `exitCode`. Pass `{ commit: message }` to save what the command changed as one commit; `committed` is then the new commit.

```ts
import { Skywake } from "skywake";
import { shell } from "skywake/bash";

const app = new Skywake().disk("app");
await app.write({
  "src/auth.ts": "// TODO: expire sessions\nexport const login = () => {};\n",
  "src/cart.ts": "// TODO: keep the cart across devices\nexport const cart = [];\n",
});

const work = await app.fork("agent-1");
const sh = shell(work);

const found = await sh.run("rg -n TODO src");
console.log(found.stdout);

const wrote = await sh.run("rg -o 'TODO: .*' src | sort > TODO.md && wc -l TODO.md", {
  commit: "List the TODOs",
});
console.log(wrote.stdout, wrote.committed);

console.log(await work.read("TODO.md"));
await work.mergeInto("main");
```

Give each agent its own [fork](../filesystem/branches.mdx), and merge it when the work is good.

## Use it as a tool [#use-it-as-a-tool]

`bashTool` from `skywake/ai` turns a disk handle (or a shell) into a [Vercel AI SDK](https://ai-sdk.dev) tool. It needs the `ai` package and a model, so this block doesn't run in our tests:

```ts norun
import { generateText, stepCountIs } from "ai";
import { Skywake } from "skywake";
import { bashTool } from "skywake/ai";

const work = await new Skywake().disk("app").fork("agent-1");

const { text } = await generateText({
  model: "anthropic/claude-sonnet-5.5",
  tools: { bash: bashTool(work) },
  stopWhen: stepCountIs(20),
  prompt: "Find every TODO under src and list them in TODO.md",
});

await work.mergeInto("main");
```

By default each command that changed files becomes one commit, with the command as its message. Pass `bashTool(work, { commitEach: false })` to keep changes pending, and commit them yourself: open the shell with `shell(work)`, hand it to `bashTool(sh, { commitEach: false })`, and call `sh.fs.commit(message)` when the agent is done.

## Use it over MCP [#use-it-over-mcp]

The [MCP server](./mcp.mdx) has a `bash` tool too, on the same shell.

## How it behaves [#how-it-behaves]

- **One view.** The shell reads the branch as of its head when the shell first reads it, until it commits. Changes others make meanwhile appear after the next commit.
- **Writes stay pending.** They are visible to later commands at once and reach the branch only on commit, all as one commit. `sh.fs.discard()` drops them. `sh.fs.pending` counts them.
- **Copies are free.** `cp -r` and `mv` of files the shell has not changed copy no data.
- **The server answers a search** when it covers only files the shell has not changed. Over changed files, or with flags it does not know, the command runs in the shell instead and reads the files.
- **Supported server-side flags.** `rg`: `-i`, `-S`, `-n`, `-l`, `-F`, `-g`, `-e`. `grep -r`: `-i`, `-n`, `-l`, `-F`, `-E`, `-e`, `--include`. `find`: `-name`, `-type f|d`.
- **Limits.** Symlinks can be read but not made. A search stops at 1,000 matches and says so. Output over 30,000 characters is cut, with a note to narrow the command.