Commands can also be built from plain strings instead of template literals.
Category: Running commands
Languages: JavaScript, Rust
API: sh, exec, run, create, shell
Verified in: Node.js, Bun
js/examples/features/function-api.mjs
// Besides the template tag there are plain functions: sh, exec, run and create.
import { $, sh, exec, run, create } from '../../src/$.mjs';
import { example } from './_harness.mjs';
await example(
{ id: 'function-api', title: 'sh(), exec(), run() and create()' },
async ({ record }) => {
record('sh(command)', (await sh('echo from-sh', { mirror: false })).stdout);
record(
'exec(file, args)',
(await exec('echo', ['from-exec'], { mirror: false })).stdout
);
record('run(command)', (await run('echo from-run')).stdout);
// create() returns a $ with preset options.
const $quiet = create({ mirror: false, capture: true });
record('create(options)', (await $quiet`echo from-create`).stdout);
// $ itself can be called with options for the same effect.
record('$(options)', (await $({ mirror: false })`echo from-dollar`).stdout);
}
);Identical in Node.js and Bun:
# function-api — sh(), exec(), run() and create()
sh(command): "from-sh\n"
exec(file, args): "from-exec\n"
run(command): "from-run\n"
create(options): "from-create\n"
$(options): "from-dollar\n"
API: run, exec, create
rust/examples/language_features.rs
async fn function_api() -> ExampleResult {
let simple = run("echo run").await?;
let configured = exec("echo exec", quiet_options()).await?;
let mut runner = create("echo create", quiet_options());
let created = runner.run().await?;
Ok(vec![observation(
"run, exec and create",
[
simple.stdout.trim(),
configured.stdout.trim(),
created.stdout.trim(),
],
)])
}# function-api — Rust
run, exec and create: ["run","exec","create"]
Not supported — Bun.$ only accepts a tagged template; a string has to be turned back into one by hand.
await $({ input: '' })`sh -c ${'echo hi'}`; // or build a template array manuallyawait execa('echo', ['hi']); // the classic function formshell.exec('echo hi'); // strings are the only formexecFile('echo', ['hi']);