Bashlex is a Python library providing a parser for bash commands. It can take a bash command string and convert it into an Abstract Syntax Tree (AST), allowing for programmatic inspection and manipulation of shell commands. The current version is 0.18, with releases occurring roughly annually or bi-annually.
pip install bashlexVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates parsing a bash command string using `bashlex.parse()` to obtain an AST, and `bashlex.tokenize()` to get all individual tokens including operators and comments. It shows how to inspect the structure and access parts of the parsed command.
Do not expect `bashlex` to resolve environment variables, execute subcommands, or expand file paths. It provides the parsed structure, which you can then process with other tools or logic.
For comment awareness, iterate through `bashlex.tokenize()` output. For command structure without comments, `bashlex.parse()` is appropriate.
In Python 3, always pass unicode strings to `bashlex` functions. If dealing with byte strings from external sources, decode them explicitly before passing to `bashlex`.
Update exception handling code to catch the specific exception types defined in the `bashlex` module (e.g., `bashlex.ParserError`, `bashlex.LexerError`) or a more general `Exception` if broad error handling is sufficient. Consult `bashlex` documentation or source code for the correct exception classes in your installed version.
Ensure the input Bash command is syntactically complete and adheres to the subset of Bash syntax supported by bashlex. For complex scripts, try to isolate the problematic parts. Consider pre-processing the input to remove unsupported features like comments or specific types of line continuations if they are not critical for your AST analysis. Example: `import bashlex; bashlex.parse('echo hello')`Verify that `bashlex.parse()` successfully returns a list of AST nodes before attempting to access node attributes. When iterating over results, ensure you're handling only `Node` objects or checking the type if strings might be present. Example: `import bashlex; parts = bashlex.parse('ls -l'); for node in parts: print(node.kind)`Install the package using pip: `pip install bashlex`. If using a virtual environment, ensure it is activated before running your script. If the package is installed but not found, check your Python environment's `PYTHONPATH`.
No dependency data recorded yet.