Package Exports
- unbash
Readme
unbash
Fast 0-deps bash parser written in TypeScript
Install
npm install unbashUsage
import { parse } from "unbash";
const ast = parse('if [ -f "$1" ]; then cat "$1"; fi');// Result:
{
type: "Script",
commands: [{
type: "If",
clause: { type: "Command", name: { text: "[" }, ... },
then: { type: "Command", name: { text: "cat" }, ... }
}]
}unbash vs tree-sitter-bash
tree-sitter-bash is an excellent choice if you need:
- Incremental parsing
- CST output preserving all tokens and punctuation
- Granular error recovery that wraps errors in
ERRORnodes and continues parsing
unbash might be a good fit if you prefer:
- AST output
- A zero-dependency dependendency that runs in any JS environment
- A typed TypeScript API
- Built-in parsing for command/process substitutions, coproc, Bash 5.3
${ cmd; },[[ ]],(( )), and extglob - Error recovery that collects errors instead of throwing
unbash vs sh-syntax
sh-syntax is a WASM wrapper around the robust mvdan/sh Go parser. It is highly recommended if you need:
- Support for multiple shell dialects (bash, POSIX sh, mksh, Bats)
- Built-in formatting and pretty-printing (
print)
unbash might be a good fit if you prefer:
- A zero-dependency, synchronous API
- A detailed AST with structured word parts, parameter expansions, arithmetic expressions, and test expressions
unbash vs bash-parser
bash-parser (last publish: 2017) and its fork @ericcornelissen/bash-parser (community dependency maintenance fork ❤️ now archived) might be interesting if you need:
- A POSIX-only mode that rejects bash-specific syntax
unbash might be a good fit if you prefer:
- A zero-dependency architecture
- A typed TypeScript API (ESM-only)
- Error recovery that collects errors instead of throwing
- Structured AST nodes for parameter expansions, arithmetic expressions, and
[[ ]]test expressions - Support for many additional syntax features (like herestrings, C-style for loops,
select, process substitution, etc. etc.)
Benchmarks
Relative performance comparison (on Apple M1 Pro/32GB), unbash is x times faster:
| Parser | short | advanced | medium | large |
|---|---|---|---|---|
| tree-sitter-bash (native) | 14x | 9x | 4x | 4x |
| tree-sitter-bash (WASM) | 14x | 10x | 7x | 7x |
| sh-syntax | 2008x | 1296x | 7x | 4x |
| bash-parser | 230x | N/A | N/A | N/A |
| @ericcornelissen/bash-parser | 247x | N/A | N/A | N/A |
Run the benchmarks using Node.js v22 or v24:
pnpm install
node bench/all.tsSize
unbash is under 50K minified, 12KB gzipped.
License
ISC