Tree-sitter Bash Grammar

0.25.1 · active · verified Thu Apr 09

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.

Warnings

Install

Imports

Quickstart

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'.

import tree_sitter
from tree_sitter_bash import language

# Initialize the parser and set the Bash language
parser = tree_sitter.Parser()
parser.set_language(language)

# Bash code to parse
bash_code = '''
#!/bin/bash
echo "Hello, Tree-sitter!"

# Loop example
for i in $(seq 1 3); do
  echo "Count: $i"
done
'''

# Parse the code (input must be bytes)
tree = parser.parse(bytes(bash_code, "utf8"))

# Print the S-expression representation of the syntax tree
print("\n--- S-expression Tree ---")
print(tree.root_node.sexp())

# Example: Find all command nodes
def find_nodes_by_type(node, node_type):
    nodes = []
    if node.type == node_type:
        nodes.append(node)
    for child in node.children:
        nodes.extend(find_nodes_by_type(child, node_type))
    return nodes

command_nodes = find_nodes_by_type(tree.root_node, 'command')
print(f"\nFound {len(command_nodes)} command nodes in the script.")
# For example, 'echo "Hello"' is one command, 'echo "Count"' is another.

view raw JSON →