unbash is a fast, zero-dependency library written in TypeScript for parsing Bash scripts into a structured Abstract Syntax Tree (AST). Currently stable at version 2.2.0, it differentiates itself by offering a pure JavaScript/TypeScript implementation without WASM or native bindings, providing a fully typed API, and delivering high performance, often outperforming alternatives by a significant margin. Its release cadence follows a typical Semantic Versioning approach, with updates for features and bug fixes. Key differentiators include its AST-centric output (unlike CST-focused parsers), tolerant parsing that collects errors rather than throwing exceptions, and built-in support for advanced Bash syntax such as process substitutions, coproc, `[[ ]]` test expressions, `(( ))` arithmetic evaluations, and extglob. It is designed for environments requiring a lightweight, synchronous parsing solution. While it excels at AST generation and speed, it does not offer incremental parsing, full token preservation for CSTs, or multi-shell dialect support (e.g., pure POSIX sh), features found in libraries like tree-sitter-bash or sh-syntax. It requires Node.js v14 or higher and maintains a small bundle size (13KB gzipped).
npm install unbashVerified import paths — ran on the pinned version, not inferred.
Parses a sample Bash script into an AST, checks for collected parsing errors, and then prints the AST back into a formatted Bash script string, demonstrating core functionality and tolerant error handling.
Migrate your project to use ES Modules (`"type": "module"` in `package.json`) and use `import` statements, or use dynamic `import('unbash')` in a CommonJS context.Understand that `print` is for generating a standardized output script from an AST, not for round-trip fidelity of the original script's formatting. Do not rely on it to preserve non-semantic elements.
Always check `if (ast.errors && ast.errors.length > 0)` after calling `parse()` to detect and handle any syntax issues present in the input script.
Evaluate your parsing requirements. unbash is optimized for speed and AST generation for structural analysis, not for preserving every token or supporting incremental updates.
Ensure that the scripts you are parsing are predominantly Bash-compliant. For parsing diverse shell dialects, investigate libraries that offer broader shell compatibility.
Update your import statement to `import { parse } from 'unbash';` and ensure your Node.js project is configured for ESM (e.g., by adding `"type": "module"` to your `package.json`).Use a named import specifically: `import { parse } from 'unbash';` (and similarly for `print` from `unbash/print`).This is expected behavior. The purpose of `print` is to produce a well-formatted, functional Bash script from the AST, not to recreate the input verbatim. If exact formatting preservation is critical, `unbash`'s `print` might not be suitable for that specific task.
After every call to `parse()`, you must explicitly check the `ast.errors` property. For example: `const ast = parse(script); if (ast.errors && ast.errors.length > 0) { console.error('Parsing failed:', ast.errors); }`No dependency data recorded yet.