Saturday, July 27, 2024
Google search engine
HomeUncategorizedThe Bun Shell

The Bun Shell

JavaScript is the world’s most popular scripting language.

So why is it hard to run shell scripts in JavaScript?

import { spawnSync } from "child_process";

// this is a lot more work than it could be
const { status, stdout, stderr } = spawnSync("ls", ["-l", "*.js"], {
  encoding: "utf8",
});

You could use APIs to do something similar:

import { readdir } from "fs/promises";

(await readdir(".", { withFileTypes: true })).filter((a) =>
  a.name.endsWith(".js"),
);

But, that’s still not quite as simple as a shell script:

Why existing shells don’t work in JavaScript

Shells like bash or sh have been around for decades.

Shells are a solved problem!!

  • Hacker News Commenter, probably.

But, they don’t work well in JavaScript. Why?

macOS (zsh), Linux (bash), and Windows (cmd) all have slightly different shells with different syntaxes and different commands. The commands available on each platform are different, and even the same command can have different flags and behaviors.

To date, npm’s solution is to rely on the community to polyfill missing commands with JavaScript implementations.

rm -rf doesn’t work on Windows

rimraf, the cross-platform JavaScript implementation of rm -rf, is downloaded 60 million times per week:

Environment variables like FOO=bar
RELATED ARTICLES

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments