@dsherret/shell

中文文档
0.7.2发布于 2026/10/06MITES Module构建来源已验证近 30 天下载 8万

Command execution and shell parser used by dax.

原始使用文档
文档目录5 节
  1. Install
  2. Usage
  3. Interpolating commands
  4. build$
  5. Custom commands

shell

JSR npm

Command execution and shell parser used by dax.

Most users should reach for dax — it builds on this package and adds progress bars, logging, request, which, and other conveniences. Use @david/shell directly when you want just the shell layer.

Works on Deno and Node.js.

Install

# npm
npm install @dsherret/shell

# jsr
deno add jsr:@david/shell

Usage

import $ from "@dsherret/shell";

// run a command (stdout inherits by default)
await $`echo 1 && echo 2`;

// capture output
const text = await $`echo hello`.text();
console.log(text); // "hello"

// interpolated args are escaped by default
const name = "some name with spaces";
await $`echo ${name}`;

// $.raw disables argument escaping
await $.raw`echo one two three`;

// $.rawArg opts a single value out of escaping
await $`echo ${$.rawArg("1   2   3")}`;

Interpolating commands

Interpolating a command substitutes its captured stdout, similar to $(...) in a shell:

const name = $`echo world`;
await $`echo hello ${name}`; // hello world
await $`echo 'hello ${name}'`; // works inside quotes too
await $`cat < ${name}`; // or as an input redirect

The interpolated command runs lazily when the surrounding command evaluates its arguments, so it never runs when short-circuited away (ex. the interpolated command in $`exit 1 && echo ${cmd}` doesn't run). It executes with its own configuration (env, cwd, etc.), except stdout is captured and stderr defaults to the evaluating command's stderr. If it fails, the command evaluating it fails with its exit code—add .noThrow() to the interpolated command to tolerate failure. As with $(...), that only surfaces when nothing else determines the exit code first (ex. $`echo ${cmd} | cat` exits with cat's status unless .pipefail() is set).

Like $(...), an unquoted substitution word-splits its output into multiple arguments and expands glob characters—interpolate inside quotes (ex. $`echo "${cmd}"`) to keep it a single argument.

In input redirect position, $`cat < ${cmd}` streams the command's raw stdout directly to the redirect (no capture or word splitting), while a quoted $`cat < "${cmd}"` substitutes its output as a file path to read. A streamed command's exit code only propagates when the reading command consumed all of its output—when the reading command stops early it's killed and its status ignored, like the left side of a pipe.

Neither interpolated values nor the literal command text may contain the NUL character (\0)—it's reserved as an internal delimiter, so either case throws a TypeError.

build$

build$ creates a $ bound to a specific CommandBuilder and/or with extra properties attached.

import { build$, CommandBuilder } from "@dsherret/shell";

const $ = build$({
  commandBuilder: new CommandBuilder().env("MY_VAR", "123"),
  extras: {
    add(a: number, b: number) {
      return a + b;
    },
  },
});

await $`echo $MY_VAR`; // uses the configured env
console.log($.add(1, 2)); // 3

Call $.build$(...) to derive a child $ that inherits the parent's command builder state and merges any new extras on top.

Custom commands

Register a handler to implement a built-in command:

import { CommandBuilder } from "@dsherret/shell";

const result = await new CommandBuilder()
  .registerCommand("greet", async (ctx) => {
    await ctx.stdout.writeLine(`hello ${ctx.args[0] ?? "world"}`);
    return { code: 0 };
  })
  .command("greet friend")
  .stdout("piped");

console.log(result.stdout); // "hello friend\n"

See mod.ts for the full public API: $/build$, CommandBuilder, CommandChild, CommandResult, KillController/KillSignal, ProcessTracker, escapeArg, createExecutableCommand, file helpers (create, open, FsFile), and types for command handlers, pipes, and shell results.

版本与发布记录

@dsherret/shell 共发布 4 个版本,首次发布于 2026/09/01;过去 12 个月发布了 4 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.7.2latest2026/10/061 个月前
0.7.12026/10/051 个月前
0.7.02026/09/012 个月前
0.6.02026/09/012 个月前

关于 @dsherret/shell 的常见问题

@dsherret/shell 是什么?

Command execution and shell parser used by dax.

如何安装 @dsherret/shell?

在项目目录运行 npm install @dsherret/shell,或使用 pnpm add / yarn add / bun add。

@dsherret/shell 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/dsherret__shell 是否存在。

@dsherret/shell 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@dsherret/shell 可以商用吗?

@dsherret/shell 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@dsherret/shell 的下载量有多少?

根据 npm 官方下载统计,@dsherret/shell 在 2026/10/13 – 2026/11/11 这 30 天内被下载 79,652 次,日均约 2,655 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dsherret/shell 还在维护吗?

最新版本 0.7.2 发布于 2026/10/06(1 个月前);至今共发布 4 个版本,其中过去 12 个月发布了 4 个;首次发布于 2026/09/01。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。