{"id":2320,"library":"tree-sitter-bash","title":"Tree-sitter Bash Grammar","description":"tree-sitter-bash provides the Bash grammar for use with the Tree-sitter parsing library. It enables robust, error-tolerant parsing of Bash scripts and shell code, allowing for syntax highlighting, code navigation, and refactoring tools. The current version is 0.25.1, and it maintains a regular release cadence, often aligning with updates to the core Tree-sitter project.","status":"active","version":"0.25.1","language":"en","source_language":"en","source_url":"https://github.com/tree-sitter/tree-sitter-bash","tags":["tree-sitter","parser","grammar","bash","shell","syntax-tree"],"install":[{"cmd":"pip install tree-sitter-bash","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"This package provides the Bash grammar; the `tree-sitter` Python library is required to load and use it for parsing.","package":"tree-sitter","optional":false}],"imports":[{"note":"The compiled grammar object is directly exposed as 'language' from the top-level package.","wrong":"import tree_sitter_bash.grammar","symbol":"language","correct":"from tree_sitter_bash import language"}],"quickstart":{"code":"import tree_sitter\nfrom tree_sitter_bash import language\n\n# Initialize the parser and set the Bash language\nparser = tree_sitter.Parser()\nparser.set_language(language)\n\n# Bash code to parse\nbash_code = '''\n#!/bin/bash\necho \"Hello, Tree-sitter!\"\n\n# Loop example\nfor i in $(seq 1 3); do\n  echo \"Count: $i\"\ndone\n'''\n\n# Parse the code (input must be bytes)\ntree = parser.parse(bytes(bash_code, \"utf8\"))\n\n# Print the S-expression representation of the syntax tree\nprint(\"\\n--- S-expression Tree ---\")\nprint(tree.root_node.sexp())\n\n# Example: Find all command nodes\ndef find_nodes_by_type(node, node_type):\n    nodes = []\n    if node.type == node_type:\n        nodes.append(node)\n    for child in node.children:\n        nodes.extend(find_nodes_by_type(child, node_type))\n    return nodes\n\ncommand_nodes = find_nodes_by_type(tree.root_node, 'command')\nprint(f\"\\nFound {len(command_nodes)} command nodes in the script.\")\n# For example, 'echo \"Hello\"' is one command, 'echo \"Count\"' is another.\n","lang":"python","description":"This quickstart demonstrates how to initialize a Tree-sitter parser, load the Bash grammar from `tree-sitter-bash`, and parse a simple Bash script. It then prints the S-expression representation of the syntax tree and finds all nodes of type 'command'."},"warnings":[{"fix":"Ensure essential build tools (like `build-essential` on Debian/Ubuntu, `Xcode Command Line Tools` on macOS, or a C++ build environment on Windows) are installed on your system.","message":"While `tree-sitter` is a direct dependency and installed automatically, the core `tree-sitter` library's C extensions sometimes require a C compiler and development headers (e.g., `gcc`, `clang`, `make`) to be present on the system. If these build tools are missing, `pip install tree-sitter-bash` might fail during the `tree-sitter` dependency installation.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always encode your string source code to bytes before parsing, typically using `bytes(your_code_string, 'utf8')` or `your_code_string.encode('utf8')`.","message":"The `parser.parse()` method strictly expects input as `bytes`, not a Python `str`. Passing a `str` will result in a `TypeError`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Periodically review the grammar's `grammar.js` or consult the `tree-sitter` documentation/changelog. Make your tree traversal logic robust to minor changes, e.g., by checking `node.type` and `node.is_named` rather than relying on exact string matches for all nodes.","message":"Minor updates to the underlying `tree-sitter` library or changes in the `tree-sitter-bash` grammar definition (even across patch versions) can subtly alter the generated syntax tree structure. This includes changes to node types, field names, or the presence/absence of anonymous nodes. Code relying on specific tree traversals or node names might break.","severity":"breaking","affected_versions":"All versions, especially across minor updates (e.g., 0.20.x to 0.25.x)"},{"fix":"Install the specific `tree-sitter-*` package for each language you intend to parse, or use a more general solution like `tree-sitter-languages`.","message":"This package (`tree-sitter-bash`) provides *only* the Bash grammar. To parse other programming languages, you will need to install separate `tree-sitter-*` grammar packages (e.g., `tree-sitter-python`) or utilize libraries like `tree-sitter-languages` which bundle many pre-compiled grammars.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}