Skip to main content

Template literal commands with variable interpolation

Deno Sandbox provide a sandboxed environment for evaluating JavaScript code. This is useful for evaluating code that is not trusted or for testing code that is not safe to run in the main runtime.

You can use template literal commands with variable interpolation in a sandbox.

import { Sandbox } from "@deno/sandbox";

await using sandbox = await Sandbox.create();

// Variables are automatically escaped
const filename = "file with spaces.txt";
const content = "Hello, world!";
await sandbox.sh`echo ${content} > ${filename}`;

// Arrays are expanded to multiple arguments
const files = ["file1.txt", "file2.txt", "file3.txt"];
await sandbox.sh`rm ${files}`;

// Get JSON output
const data = await sandbox.sh`echo '{"count": 42}'`.json<{ count: number }>();
console.log(data.count); // → 42

For more information, see the Deno Sandbox documentation.

Did you find what you needed?

Privacy policy