shellcheck-py

0.11.0.1 · active · verified Thu Apr 09

shellcheck-py is a Python wrapper designed to provide a pip-installable `shellcheck` binary. It internally downloads the pre-built `shellcheck` static analysis tool for shell scripts, making it available in your environment without needing system-level package managers. The current version is 0.11.0.1. The library's release cadence appears to be driven by updates to the underlying `shellcheck` tool and Python compatibility.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically use the `shellcheck` binary installed by `shellcheck-py` via Python's `subprocess` module. It creates a simple shell script with a known `shellcheck` warning (SC2034) and then invokes `shellcheck` to analyze it, printing the JSON output and exit code.

import subprocess
import os

# Create a dummy shell script for demonstration
script_content = '''#!/bin/bash

# SC2034: foo appears unused. Verify use (or export if used externally).
foo="bar"

echo $foo
'''

with open("test_script.sh", "w") as f:
    f.write(script_content)

os.chmod("test_script.sh", 0o755) # Make it executable

print("Running shellcheck on test_script.sh:")
try:
    # Run shellcheck, capturing output. -f json for machine-readable output
    result = subprocess.run(
        ['shellcheck', '-f', 'json', 'test_script.sh'],
        capture_output=True,
        text=True,
        check=False # Don't raise an exception for non-zero exit codes (warnings/errors)
    )
    print("Standard Output:")
    print(result.stdout)
    if result.stderr:
        print("Standard Error:")
        print(result.stderr)
    print(f"Exit Code: {result.returncode}")
except FileNotFoundError:
    print("Error: 'shellcheck' command not found. Ensure shellcheck-py is installed correctly.")
except Exception as e:
    print(f"An error occurred: {e}")
finally:
    # Clean up the dummy script
    if os.path.exists("test_script.sh"):
        os.remove("test_script.sh")

view raw JSON →