<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>~iany/ Bun</title><link>https://blog.iany.me/tags/bun/</link><description>Recent content in Bun «~iany/»</description><language>en-US</language><managingEditor>me@iany.me (Ian Yang)</managingEditor><webMaster>me@iany.me (Ian Yang)</webMaster><copyright>CC-BY-SA 4.0</copyright><lastBuildDate>Sat, 07 Feb 2026 00:00:00 +0800</lastBuildDate><atom:link href="https://blog.iany.me/tags/bun/index.xml" rel="self" type="application/rss+xml"/><item><title>Use Bun for Shell Scripts</title><link>https://blog.iany.me/2026/02/use-bun-for-shell-scripts/</link><pubDate>Sat, 07 Feb 2026 00:00:00 +0800</pubDate><author>me@iany.me (Ian Yang)</author><guid>https://blog.iany.me/2026/02/use-bun-for-shell-scripts/</guid><description>&lt;p&gt;I&amp;rsquo;ve moved most of my small scripts to Bun. It sidesteps Windows&amp;rsquo; lack of shebang support and the Git-for-Windows/WSL bash dance, and gives you one runtime for both ad-hoc shell-style commands and full scripts.&lt;/p&gt;
&lt;h2 id="problems-bun-solves"&gt;Problems Bun solves&lt;/h2&gt;
&lt;p&gt;Windows doesn&amp;rsquo;t execute &lt;code&gt;#!/usr/bin/env python3&lt;/code&gt; or &lt;code&gt;#!/bin/bash&lt;/code&gt;, so scripts written for Linux or macOS usually need a separate PowerShell version. Bun compiles JavaScript to native executables, so a single script can run cross-platform without shebangs.&lt;/p&gt;
&lt;p&gt;Mise &lt;a href="https://mise.jdx.dev/tasks/file-tasks.html"&gt;file tasks&lt;/a&gt; have started to support shebangs on Windows in the latest release, but they rely on system &lt;code&gt;bash.exe&lt;/code&gt;, which often points at WSL. Bun does not. You run &lt;code&gt;bun ./script.js&lt;/code&gt; and embed shell commands via &lt;code&gt;Bun.$&lt;/code&gt;; the script uses Bun&amp;rsquo;s bundled bash environment.&lt;/p&gt;
&lt;h2 id="why-bun-for-shell-scripts"&gt;Why Bun for shell scripts&lt;/h2&gt;
&lt;p&gt;The command &lt;code&gt;bun build ./script.ts --compile --outfile script&lt;/code&gt; &lt;a href="https://bun.com/docs/bundler/executables"&gt;produces a single binary&lt;/a&gt;, so no shebang or interpreter is needed on Windows. The &lt;code&gt;Bun.build&lt;/code&gt; API is available too; I use a &lt;code&gt;build.js&lt;/code&gt; script to walk a directory and compile all executables. Pre-built binaries start quickly—Bun&amp;rsquo;s docs cite ~5ms vs Node&amp;rsquo;s ~25ms for a simple script, which helps for small, frequently run scripts.&lt;/p&gt;
&lt;p&gt;Bun has strong support for shell-style scripting. &lt;a href="https://bun.com/docs/runtime/shell"&gt;Bun Shell&lt;/a&gt; runs bash commands; the main entry point is &lt;code&gt;Bun.$&lt;/code&gt;, which constructs shell commands from template literals for safe interpolation, similar to &lt;a href="https://github.com/google/zx"&gt;google/zx&lt;/a&gt;. On Windows, Bun ships with MSYS, so you get a consistent bash environment and common Linux CLI tools without extra setup.&lt;/p&gt;
&lt;p&gt;Other useful features:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Bun.secrets&lt;/code&gt;—Securely store and retrieve secrets using the OS keystore (Windows Credential Manager, macOS Keychain, Linux libsecret). &lt;a href="https://bun.com/docs/api/secrets"&gt;Secrets - Bun&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Built-in support for glob patterns, YAML parsing, ANSI colors, and more. &lt;a href="https://bun.com/docs/runtime"&gt;Bun Runtime&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="short-example"&gt;Short example&lt;/h2&gt;
&lt;pre&gt;&lt;code class="language-javascript"&gt;import { $ } from &amp;quot;bun&amp;quot;;
const out = await $`echo hello`.text();
console.log(out); // hello
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Run it with &lt;code&gt;bun run script.js&lt;/code&gt; or &lt;code&gt;bun ./script.js&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id="obsidian-notes"&gt;Obsidian Notes&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://kb.iany.me/para/lets/d/Development&amp;#43;Environment/JavaScript&amp;#43;Shell&amp;#43;Scripting"&gt;JavaScript Shell Scripting&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description><category domain="https://blog.iany.me/post/">Posts</category><category domain="https://blog.iany.me/tags/bun/">Bun</category><category domain="https://blog.iany.me/tags/javascript/">JavaScript</category><category domain="https://blog.iany.me/tags/script/">Script</category><category domain="https://blog.iany.me/tags/windows/">Windows</category><category domain="https://blog.iany.me/tags/dev-environment/">Dev Environment</category></item></channel></rss>