Back to articles
Bun Shell Has a Free API: Write Shell Scripts in JavaScript That Actually Work

Bun Shell Has a Free API: Write Shell Scripts in JavaScript That Actually Work

via Dev.to JavaScriptAlex Spinov

Bash scripts break on spaces in filenames. Node's child_process is verbose. Bun Shell gives you shell scripting in JavaScript — safe, cross-platform, and actually readable. What Is Bun Shell? Bun Shell ( Bun.$ ) is a shell-like API built into Bun. It combines the convenience of shell scripting with the safety of JavaScript. import { $ } from " bun " // Run shell commands await $ `echo "Hello from Bun Shell!"` // Capture output const result = await $ `ls -la` . text () console . log ( result ) // Pipe commands await $ `cat data.json | jq '.users[]'` // Template literals are safe (auto-escaped!) const filename = " file with spaces.txt " await $ `cat ${ filename } ` // Works! No quoting issues // Environment variables await $ `echo $HOME` Safe by Default // Bash: user input can break everything // rm -rf $user_input ← if user_input is "/ --no-preserve-root", disaster // Bun Shell: template variables are auto-escaped const userInput = " / --no-preserve-root " await $ `echo ${ userInput } `

Continue reading on Dev.to JavaScript

Opens in a new tab

Read Full Article
2 views

Related Articles